I no longer work at Microsoft, so please don't bother leaving a comment here or trying to contact me through my MSDN blog.
You can find my new blog at http://www.technologytoolbox.com/blog/jjameson. My new site also provides copies of all posts from my MSDN blog.
This post continues on the original post for my simple, but highly effective approach to logging and the follow-up post which introduced configuring logging for console applications.
Obviously not all solutions are simple console-based applications. With ASP.NET Web services and applications -- including Microsoft Office SharePoint Server (MOSS) and Windows SharePoint Services (WSS) -- you can still view log messages from the Logger class very easily on a per-request basis.
Note that the System.Web.WebPageTraceListener can be specified in Web.config to enable logging to the ASP.NET tracing feature:
<source name="defaultTraceSource" switchName="allTraceLevel">
<add name="webPageTraceListener" />
<add type="System.Web.WebPageTraceListener, System.Web, Version=126.96.36.199,
name="webPageTraceListener" traceOutputOptions="None" />
<add name="allTraceLevel" value="All" />
<add name="verboseTraceLevel" value="Verbose" />
<add name="infoTraceLevel" value="Info" />
<add name="warningTraceLevel" value="Warning" />
<add name="errorTraceLevel" value="Error" />
<add name="offTraceLevel" value="Off" />
Also note that ASP.NET tracing must be enabled using the <trace> element within <system.web>:
<trace enabled="true" pageOutput="false" requestLimit="50" localOnly="true" />
Be sure to set the requestLimit high enough to enable access to the page trace you are interested, but also be aware that you can easily clear the captured traces and then browse to the page of interest again.
Once configured, browse to the page on the site. Then modify the URL to browse to Trace.axd (e.g. http://fabrikam-local/Trace.axd) to display the Trace Viewer.
Locate the request that you want to view log messages for and click the corresponding View Details link.
Notice that the WebPageTraceListener even formatted the warning message in red. How cool is that?! [For all you SharePoint developers out there, compare this with "diving" into the ULS logs to find an error or warning for a particular page request!]
Be aware that the Web.config file you use for SharePoint applications varies depending on whether you want to view log messages for a content page or for an application (i.e. _layouts) page. This is covered in my next post.