You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Ron Grabowski <ro...@yahoo.com> on 2005/05/23 18:16:36 UTC

log4netContrib.AspNetPatternConverters

I've written a collection of PatternConverter classes to make log4net
1.2.9 beta play nicely with Asp.Net. They allow for properties of the
Application, Cache, Context, HttpRuntime, Request, Response, and
Session to be used to configure log4net. Values from these objects may
also be inserted into log messages without first having to place the
items in the MDC in code. This may offer more run-time style debugging
of applications without having to take the application down, add
additional logging, recompile, then redeploy. The ability to access
sub-properties also exists (i.e. Response.Cookies["UserId"].Domain).

Here are some examples:

 <conversionPattern
  value="%p %d %aspnet-session{UserId} - %m%n" />

<conversionPattern
 value="%p %d %aspnet-request{RawUrl} - %m%n" />

<conversionPattern
 value="%p %d %aspnet-request-querystring{ProductId} - %m%n" />

<conversionPattern
 value="%p %d %aspnet-context-user-identity{Name} - %m%n" />

<conversionPattern
 value="%p %d %aspnet-context-user-identity{IsAuthenticated} - %m%n" />

If those names are to long, you can easily rename them to anything
you'd like:

<conversionPattern
 value="%p %d %request{RawUrl} - %m%n" />

<conversionPattern
 value="%p %d %querystring{ProductId} - %m%n" />

<conversionPattern
 value="%p %d %user-identity{Name} - %m%n" />

<conversionPattern
 value="%p %d %user-identity{IsAuthenticated} - %m%n" />

Its also possible to access more than one thing at a time:

<conversionPattern
 value="%p %d %request{RawUrl} %session{UserId} - %m%n" />

Here are more complete examples:

http://www.ronosaurus.com/log4net/AspNetPatternConverters1.txt

And source code:

http://www.ronosaurus.com/log4net/log4netContrib.AspNetPatternConverters.zip

This is just the first version so if things aren't doing what you'd
expect, let me know and I'll look into it. Currently, if the requested
item or property cannot be found, an empty string is outputted. 

Comments, questions, cool, not cool, don't care?

- Ron