WCF logging messages - wcf

I have a WCF service (hosted by IIS webpage) that is working but I want to write more info out to a log file.
I inherited code and it had some simple logging to a file. That worked on the original developers machine but I don't get anything written out. Note it reads the log file path from the config which I assume was the web config but just in case I hard coded the location for now "c:\temp\logfile.log".
I gave full permissions to temp to ASPNET and then USERS thinking it was a permissions issues.
Also I noticed there was Tracing and Message logging built into WCF. Tried that but not sure if I'm doing it correctly.
Any ideas about the simple file writing?
Should I be doing the built in logging and if so any simple examples?
Thanks!

To configure Logging:
Configure Logging
1. In the Configuration Editor, select the Diagnostics node.
2. In the right pane, click Enable MessageLogging.
This will create ServiceModelMessageLoggingListener and
System.ServiceModel.MessageLogging nodes under the Listeners and Sources
folders, respectively.
3. In the left pane, select MessageLogging under the Diagnostics node.
4. Set the LogMessagesAtServiceLevel attribute to True by choosing this option
from the drop-down list.
5. In the left pane, select ServiceModelMessageLoggingListener under the
Listeners node.
Note the default value of the InitData attribute, which is set to
c:\inetpub\wwwroot\WCFService\web_messages.svclog, the location where the
message will be logged.
via WCF Security Guidance.

Use the following in <system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
and use the following in <configuration>
<system.diagnostics>
<sources>
<source name ="System.ServiceModel" switchValue="Information, ActivityTracing">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name ="System.ServiceModel.MessageLogging"
switchValue="Verbose, ActivityTracing">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name ="System.Runtime.Serialization" switchValue="Verbose">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener"
traceOutputOptions="LogicalOperationStack"
initializeData="C:\logs\RestAPISvcLog\Traces.svclog" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
This will create log file and you can view directly that with svctracelog viewer (which comes with VS).
For more info refer this -- http://msdn.microsoft.com/en-us/library/ms730064(v=vs.110).aspx

Use the EventViewer logging instead, but you will have to make sure your installer can create the appropriate EventViewer source.

Maybe you should use that Configuration Editor-Tool "SvcConfigEditor.exe" (see description on MSDN). This allows to configure WCF-logging in a quiet easy and structured way.
The log files can then be analysed with the Service Trace Viewer-Tool "SvcTraceViewer.exe" (see on MSDN).
Both tools are bundled with Visual Studio an can be downloaded from Microsoft.

To write to the log file, make sure that the identity running your web application has write access to the log directory.
IIS7:
You can find the identity in the IIS management console. Select the application pool that your web application is using. Click on Advanced Settings. In the properties window, look for the identity field. It may say Network Service. This is the account that needs write permission to your log output folder.
IIS6:
Same as IIS7 except right click on the app pool and select properties. The properties window of IIS6 will have an Identity tab.
If you already have a log file in this directory, try deleting it and letting the framework create it.
Hope this helps.

Related

Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'aspNetCore

I have recently published my ASP.NET Core application to my host. I am hitting a HTTP Error 500.19.
IIS 8.5 says the issue is:-
"Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'aspNetCore'"
It also highlights this key add line in my system.webServer config:-
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
</handlers>
I'm not really sure what to do on this. It looks as though there is a duplicate instance of this, so I have tried renaming this but it still asks to add this again?
Here is my web.config:-
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
<system.net>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>
</configuration>
The answer above didn't work for me, however DavidG's comment did solve my problem, so going to post as an answer in case it helps someone else.
For me, I was not running it as a sub-application, and a project that had been working for me no issue for over a year suddenly stopped working with this issue. Still not sure what changed. When I commented out or removed the <add name="aspNetCore".../> the error persisted, and then that line got automatically re-added.
To solve the problem, I added <remove name="aspNetCore" /> to the config file, right above the <add name="aspNetCore"... /> entry, and things started working again.
To continue running on IIS EXPRESS, go on root folder where the .sln file stays.
go to delete file from .vs\config\applicationhost.config or save
it in a temporary place if you have something there.
Close/Re Open VS Studio, run again, will work.
If you need to add something back from save applicationhost.config, just compare those two, but I don't see what you could have there.
None of the suggested solutions worked for me unfortunately. By some miracle I learned that my applicationhost.config file had been modified in an unfortunate matter, making that "Cannot add duplicate collection entry" error appear when I navigated to a specific page in my .NET Core website application.
Under the <sites> tag in applicationhost.config, I had the following:
<site name="MyWebsite" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\___\solutionname\MyWebsite" />
</application>
<application path="/SomePage" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\___\solutionname\MyWebsite" />
</application>
<bindings>
<binding protocol="https" bindingInformation="*:12345:localhost" />
</bindings>
</site>
The 500.19 HTTP error was shown to me when I navigated to the page "/SomePage". As you can see, for some reason there was a separate <application> tag for this specific page. I have no idea why.
I removed that entire <application> tag for the "/SomePage" path, and everything started working again.
I faced this issue with vs 2017 on a project that was working fine without changing the web.config. Looking at this posts I realized that it might be an IIS express issue and I solved simply deleting .vs folder and restarting vs.
I had the same problem and in my case commenting the line
<add name="aspNetCore"...
solved the issue and brought up the question "why is it working without AspNetCoreModule".
The problem in my case was that I was adding the site as a sub-application in defaultwebsite and it was located in the wwwrootfolder. I think the config was automatically picked up by the defaultwebsite and applied for all sub-application sites.
This link helped
So the solution was to move it as a separate site on another port.
In my case, the issue was caused by putting a path in the Debug Tab of my web project so that the app would open at a particular page. This causes two silent additions to the file .vs\config\applicationhost.config, similar to the one observed by eightx2.
In :
<add name="api AppPool" managedRuntimeVersion="" />
In :
<application path="/blah" applicationPool="api AppPool">
<virtualDirectory path="/" physicalPath="your-path\src\your-proj" />
</application>
where a similar entry already exists. This is the root of the problem.
The error message, unfortunately, is completely misleading.
Solution is to rename applicationhost.config, restart VS, and let it rebuild the file. This is why Ricardo's solution of deleting the entire .vs folder also works.
That error is because there is a root file in ASP.NET Core that is called ".vs\config\applicationhost.config"
Initially it has 67 keys. You can see it for yourself here, in the Configuration Editor.
This file called ".vs\config\applicationhost.config" has the default settings carried by the Web.config to be able to work, and one of them is that handler.
You can also see it here.
The problem is that that file has that handler and what you publish is going to inherit that handler.
You have two solutions, comment on the line of your published web.config or delete that handler from the ".vs\config\applicationhost.config"
I just had this one, it turns out I had changed the App Url in Debug settings for the website Properties, in order to load a specific page (wrong but happened).
In IIS it automatically created a new application under the test domain called About (in this case).
Removing the rogue IIS application under the domain solves the issue as it doesn't attempt to reload the same web.config when navigating to the page.
I commented the below statement
<verbs allowUnlisted="true">
And its works
If you're running your site in IIS, check if your Application Pool has a "duplicate" Application attached.
I had this same problem debugging my site in IIS. While troubleshooting I found that the Application Pool for my site showed 2 applications connected to it. I checked each of my sites to make sure I hadn't accidentally assigned one of them to the same app pool but they were all correct. So I deleted my site in IIS and checked the app pool which then showed 0 applications. I recreated the site in IIS, attached it to the application pool which then showed only the 1 application. Restarted the site and it worked correctly.
Not sure how that phantom application got attached to my app pool but that's what was causing the duplicate aspNetCore key in my case.
I have a parent web application with multiple child applications.
This is what worked for me:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments="<pathToDll>" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
Basically, wrap the system.webServer element of the parent website with the location element and set inheritInChildApplications: <location path="." inheritInChildApplications="false">.
Then the child websites can stay the same and you do not need to add <remove name="aspNetCore" /> to each child website's web.config.
Note that this is in the project file:
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
And it gets changed to this during publish:
<aspNetCore processPath="dotnet" arguments="<pathToDll>" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />

ImageResizing Not Working In Hosting

I have an asp.net mvc project. I installed imageresizing nuget packages. It's working in local.
When I published to my hosting then it's not working.
I set IIS situation integrated and classic but still didn't work.
I couldn't solve, can you help please? Thanks.
The following definitions are exist in web.config
<resizer>
<plugins>
<add name="MvcRoutingShim" />
<add name="DiskCache" />
<add name="SimpleFilters" />
<add name="PrettyGifs" />
</plugins>
</resizer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
</modules>
The self-diagnostics page at /resizer.debug explains the configuration issues present on your hosting environment
Image resizer diagnostic sheet 24.11.2014 22:15:04
2 Issues detected:
(Critical): Grant the website SecurityPermission to call UrlAuthorizationModule.CheckUrlAccessForPrincipal
Without this permission, it may be possible for users to bypass UrlAuthorization rules you have defined for your website, and access images that would otherwise be protected. If you do not use UrlAuthorization rules, this should not be a concern. You may also re-implement your security rules by handling the Config.Current.Pipeline.AuthorizeImage event.
DiskCache(ConfigurationError): Not working: Your NTFS Security permissions are preventing the application from writing to the disk cache
Please give user read and write access to directory "D:\inetpub\adafirin.com\www\imagecache" to correct the problem. You can access NTFS security settings by right-clicking the aformentioned folder and choosing Properties, then Security.

Log SOAP message (response created by server) into database on server side

I want to log Outgoing (SOAP) messages generated by Web service in response of request sent by client (on server side).
Using Service model clients are able to log the response into the database by applying settings into the web.config of the web application e.g.
< system.servicemodel >
< extensions>
< behaviors>
< bindings> ...
Please guide me how to achieve logging and configuration on server side.
You can enable diagnostic tracing :
Just need to add a section in web.config -
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "F:\Sony\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
http://sonyarouje.com/2011/11/10/diagnostic-trace-svclog-configuration-for-wcf/
http://msdn.microsoft.com/en-us/library/ms732023.aspx
Also check this related question How to turn on WCF tracing?
To see the actual SOAP envelope that is passed around
http://litemedia.info/debug-soap-request-and-response-in-wcf
Also found one more similar question How can I enable WCF logging so that it writes to a Database?

WCF service slow response after less than 5 minutes idle (4.0 on Azure)

I have a WCF service with 2 endpoints using BasicHttpBinding running on an instance in Azure. When the service is called for the first time it takes 13-16 seconds for a response (which is to be expected) and subsequent requests are adequate at between 150ms and 1000ms depending on the request type (I am using soapUI for testing).
But if I do not send any requests for a few minutes (less than 4 or 5 minutes) the service instance does not appear to be getting reused as it is taking as long as the first request of 13+ seconds.
I have tried "InstanceContextMode = InstanceContextMode.Single" in my ServiceBehavior(s) but that did not fix the problem.
In the trace I can see that the thread id changes whenever the service gives a slow response.
I have also logged the time from when Application_BeginRequest is hit to the start of the method being called by the client which seems to be the largest delay during these 'slow' calls.
I have some fairly large classes representing business interfaces and am using Entity Framework for a MySQL database running on an Azure VM. (Although I don't think this is necessarily an Azure issue).
The slow responses are the same whether I am calling a complex message type linked to DB operations or just a simple "Hello World" response.
Even if a new instance of the service is required should it take as long as an initial load after an IIS restart or application pool recycle?
Would doing some magic using wrappers for Static classes help? (I'm thinking not).
Built with C# 4.0 / VS2010 Professional
There can be lots of reasons why this is happening. I suggest you start with some tracing in your WCF service.
Add some logging in your application using TraceSource:
private TraceSource ts = new TraceSource("MyApp");
public string GetData(int value)
{
ts.TraceInformation("GetData called with {0}", value);
return string.Format("You entered: {0}", value);
}
Configure the TraceSource in your web.config together with the System.ServiceModel source.
<system.diagnostics>
<sources>
<source name="MyApp" switchValue="All">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel" switchValue="All" propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="Logs.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
Open the svclog file and you'll be able to see a detailed overview of everything that's happing in your service and how long each little step takes. This should help you to poinpoint the issue.

wcf trying to set up tracing to debug, not writing to log file

here's my web.config, running a WCF service in an application on IIS7, but nothing is being written to the specified file. permission on the file has been granted for everyone.
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing, error, warning, critical" propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="c:\log\tracestext.log" />
</listeners>
</source>
</sources>
</system.diagnostics>
I can add a service reference just fine.
I then try to call the service from a windows app and, after a few minutes, get an error on the machine running the windows app "Client is unable to finish the security negotiation within the configured timeout (00:00:00). The current negotiation leg is 1 (00:00:00)."
but absolutely nothing is written to the trace log file specified in config.
Is there something else I need to do to enable tracing? thanks for your help
EDIT: "sources" section now matches the section recommended here: http://msdn.microsoft.com/en-us/library/aa702726.aspx
I've added the "diagnostics . messagelogging" section to "system.servicemodel"
and the event viewer shows: "Message Logging has been turned on. Sensitive information may be logged in the clear, even if it was encrypted on the wire: for example, message bodies.
Process Name: w3wp
Process ID: 1784
"
but the log file is still empty
Yes - you've only just defined some .NET tracing source and listeners - but you haven't instructed WCF yet to actually do the tracing!
You also need:
<system.serviceModel>
<diagnostics>
<messageLogging
logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="false"
logMalformedMessages="true" logEntireMessage="true"
maxSizeOfMessageToLog="65535000" maxMessagesToLog="500" />
</diagnostics>
</system.serviceModel>
These two sections of config combined should do it!
In order to get your messages written back to the log file right away, you might want to add a setting to your <system.diagnostics> section:
<system.diagnostics>
... everything you already have....
<trace autoflush="true" />
</system.diagnostics>
To write to the log file, make sure that identity running your web application has write access to the log directory.
You can find the identity in the IIS 7 management console. Select the application pool that your web application is using. Click on Advanced Settings... In the properties window, look for the identity field. It may say Network Service. This is the account that needs write permission to your log output folder.
If you already have a log file in this directory, try deleting it and letting the framework create it.
Hope this helps.
Make sure you have configured both the system.diagnostics and the
System.serviceModel/diagnostics sections configured.
Make sure you have them configured in the correct App.config/Web.config file. The thing to note is that multiple
config files may exist in a project, and the one used depends on the
Build Configuration.
Personally I had the very same symptom until I noticed that I put the sections under app.config (in my case, client side tracing), instead of app.DebugLocal.config. The later was used as my build configuration was set to DebugLocal.
Probably the issue is due to permission to write in the log directory specified in your config file.
If you are'nt sure wich is the user in the context, give write permission to all machine users.
Right click in log directory
Click in the "Security" tab
Click edit
On "Group Names or Users" section, select "Users MachineName\Users"
On "Permissions" section grant permission to write
It worked fine for me.