I want to use below code with a website. Which config sections
I should add to web.config
to log the output into a file or windows eventlog ?
using System.Diagnostics;
// Singleton in real code
Class Logger
{
// In constructor: Trace.AutoFlush = false;
public void Log(message)
{
String formattedLog = formatLog(message);
Trace.TraceInformation(formattedLog);
Trace.Flush();
}
}
To activate tracing, you must create a trace listener and set a trace level other than "Off" for the selected trace source in configuration; otherwise, WCF does not generate any traces. If you do not specify a listener, tracing is automatically disabled.
Configure Tracing config file of the WCF service library and add the configuration for system. diagnostics. In this configuration we are using "Critical, Information, ActivityTracing" as switch value. This will generate traces for all unhandled exceptions, successful execution and communication between two components.
Right-click a server. To view the trace log file, select Open Log Files > Trace File from the menu. To view the messages log file, select Open Log Files > Message Log File from the menu.
If you want to disable the trace source, you should use the logMessagesAtServiceLevel , logMalformedMessages , and logMessagesAtTransportLevel attributes of the messageLogging element instead. You should set all these attributes to false.
You should use system.diagnostics
section.
Here's example from MSDN for text file:
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
This is for system events log: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlogtracelistener.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With