In this post I am going to take the same example to
demonstrate how to enable wcf service trace. By default WCF service does not trace any messages. To
add wcf tracing, please follow below steps –
Step-1 Open your web.config file from solution explorer. The contents of
newly created Service would look a like –
<? Xml version=“1.0“?>
<configuration>
<system.web>
<compilation debug=“true“ targetFramework=“4.0“ />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!– To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment –>
<serviceMetadata httpGetEnabled=“true“/>
<!– To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information –>
<serviceDebug includeExceptionDetailInFaults=“false“/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled=“true“ />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=“true“/>
</system.webServer>
</configuration>
<system.web>
<compilation debug=“true“ targetFramework=“4.0“ />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!– To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment –>
<serviceMetadata httpGetEnabled=“true“/>
<!– To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information –>
<serviceDebug includeExceptionDetailInFaults=“false“/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled=“true“ />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=“true“/>
</system.webServer>
</configuration>
Note:- In above code tracing and message
logging both are not enabled.
<diagnostics>
<messageLogging logEntireMessage=“true“
logMessagesAtServiceLevel=“false“
logMessagesAtTransportLevel=“false“
logMalformedMessages=“true“
maxMessagesToLog=“5000“
maxSizeOfMessageToLog=“2000“>
</messageLogging>
</diagnostics>
logMessagesAtServiceLevel=“false“
logMessagesAtTransportLevel=“false“
logMalformedMessages=“true“
maxMessagesToLog=“5000“
maxSizeOfMessageToLog=“2000“>
</messageLogging>
</diagnostics>
Attribute details:-
Sr. No
|
Attribute
|
Description
|
1
|
logEntireMessage
|
“true” value indicates that the entire
message should be logged.
|
2
|
logMessagesAtServiceLevel
|
“true” means service-level messages will be
logged.
|
3
|
logMessagesAtTransportLevel
|
Transport level
messages will be logged if set to “true”.
|
4
|
logMalformedMessages
|
Malformed
message should be logged if set to “true”
|
5
|
maxMessagesToLog
|
This is the
number that tells how many messages to be logged.
|
6
|
maxSizeOfMessageToLog
|
The value indicates
the maximum size of the message log. Default is 256Kb
|
Step-3 As mentioned above, we need to define trace source and TraceListener to activate wcf tracing. In below code snippet we have added both Trace Source mentioned earlier and created an object of a tracelistener named as “TextLogger” which will act as a shared tracelistener for both trace sources.Copy and paste below code snippet just after </system.webServer> tag highlighted in first
<system.diagnostics>
<trace autoflush=“true“ indentsize=“4“ />
<sources>
<source name=“System.ServiceModel“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
<source name=“System.ServiceModel.MessageLogging“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
</sources>
<sharedListeners>
<add name=“textLogger“
type=“System.Diagnostics.TextWriterTraceListener“
initializeData=“C:WCF_logswcf_svclogging.txt“ />
</sharedListeners>
</system.diagnostics>
<trace autoflush=“true“ indentsize=“4“ />
<sources>
<source name=“System.ServiceModel“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
<source name=“System.ServiceModel.MessageLogging“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
</sources>
<sharedListeners>
<add name=“textLogger“
type=“System.Diagnostics.TextWriterTraceListener“
initializeData=“C:WCF_logswcf_svclogging.txt“ />
</sharedListeners>
</system.diagnostics>
Please note : Here we have set autoflush=“true“. If we don’t do that there will be a
possibility that the trace file would be corrupted. It would open in any text
editor, however WCF trace viewer would not be able to open such corrupted file.
Step-5 Complete code of Web.config file used in above example for MessageLogging and TraceListener is available here –
<?xml version=“1.0“?>
<configuration>
<system.web>
<compilation debug=“true“ targetFramework=“4.0“ />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!– To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment –>
<serviceMetadata httpGetEnabled=“true“/>
<!– To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information –>
<serviceDebug includeExceptionDetailInFaults=“false“/>
</behavior>
</serviceBehaviors>
</behaviors>
<diagnostics>
<messageLogging logEntireMessage=“true“
logMessagesAtServiceLevel=“false“
logMessagesAtTransportLevel=“false“
logMalformedMessages=“true“
maxMessagesToLog=“5000“
maxSizeOfMessageToLog=“2000“>
</messageLogging>
</diagnostics>
<serviceHostingEnvironment multipleSiteBindingsEnabled=“true“ />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=“true“/>
</system.webServer>
<system.diagnostics>
<trace autoflush=“true“ indentsize=“4“ />
<sources>
<source name=“System.ServiceModel“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
<source name=“System.ServiceModel.MessageLogging“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
</sources>
<sharedListeners>
<add name=“textLogger“
type=“System.Diagnostics.TextWriterTraceListener“
initializeData=“C:WCF_logswcf_svclog.txt“ />
</sharedListeners>
</system.diagnostics>
</configuration>
<system.web>
<compilation debug=“true“ targetFramework=“4.0“ />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!– To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment –>
<serviceMetadata httpGetEnabled=“true“/>
<!– To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information –>
<serviceDebug includeExceptionDetailInFaults=“false“/>
</behavior>
</serviceBehaviors>
</behaviors>
<diagnostics>
<messageLogging logEntireMessage=“true“
logMessagesAtServiceLevel=“false“
logMessagesAtTransportLevel=“false“
logMalformedMessages=“true“
maxMessagesToLog=“5000“
maxSizeOfMessageToLog=“2000“>
</messageLogging>
</diagnostics>
<serviceHostingEnvironment multipleSiteBindingsEnabled=“true“ />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=“true“/>
</system.webServer>
<system.diagnostics>
<trace autoflush=“true“ indentsize=“4“ />
<sources>
<source name=“System.ServiceModel“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
<source name=“System.ServiceModel.MessageLogging“ switchValue=“All“>
<listeners>
<add name=“textLogger“ />
</listeners>
</source>
</sources>
<sharedListeners>
<add name=“textLogger“
type=“System.Diagnostics.TextWriterTraceListener“
initializeData=“C:WCF_logswcf_svclog.txt“ />
</sharedListeners>
</system.diagnostics>
</configuration>
In the above post I ecplained how to enable WCF trace. I hope you enjoyed this post so please comment your feedback and queries. Thanks You.
No comments:
Post a Comment