We've done this before using log4net. Here are the high level steps:
Create a log4net section in your web.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
and...
...
</system.web>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="c:\mysvc.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<level value="DEBUG" />
<!--<priority value="DEBUG" />-->
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
...
In your Global.asax.cs call the log4net Configure()
protected void Application_Start(Object sender, EventArgs e)
{
log4net.Config.DOMConfigurator.Configure();
}
At the top of your web service .cs file, declare an instance of a log4net ILog
public class MyService : System.Web.Services.WebService {
{
private static readonly ILog log = LogManager.GetLogger(typeof(MyService));
In your web service methods, trap exceptions and log them
[WebMethod]
public void DoSomething()
{
try
{
// business logic
}
catch(Exception ex)
{
log.Error("Something bad happened", ex);
throw;
}
}
After these steps, in the example above any exceptions will appear in c:\mysvc.log