2

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!

cody
  • 21
  • 1
  • 2
  • .net core here - https://stackoverflow.com/questions/38868940/wcf-client-logging-dotnet-core – Kaido Oct 27 '21 at 09:28

5 Answers5

3

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.

Ta01
  • 31,040
  • 13
  • 70
  • 99
1

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

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • I have a WCF service hosted on windows service. I used the above configuration but it doesn't log any messages. Is there any additional configuration required? – developer May 28 '21 at 11:16
0

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

Chris O
  • 5,017
  • 3
  • 35
  • 42
0

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.

0

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.

Mark Good
  • 4,271
  • 2
  • 31
  • 43