Including additional properties using PostSharp, Log4Net and Unity

341 views Asked by At

I have a WCF application which uses Unity (3.5.1404) for DI via Unity.Wcf (3.5.1404) and PostSharp.Patterns.Diagnostics.Log4Net (4.1.14) with Log4Net (2.0.3) for logging via UnityLog4NetExtension (1.1).

This all works exactly as expected and I get logging to both Event Log and SQL Server.

However, I can only see how to include the built in Log4Net properties - date, thread, logger, level, message, exception and location.

I want to add the server hostname and other custom properties into the logs.

I know that I can include extra properties in the logs via the Log4Net config - %property{host} - but I cannot work out where to place the code to do this via PostSharp's woven log code.

Is this possible?

Will I have to write a custom aspect to do this?

1

There are 1 answers

1
shriek On BEST ANSWER

The easiest way this could be done would be to place the code in your Global.asax like this.

protected void Application_Start(Object sender, EventArgs e) {
    log4net.GlobalContext.Properties["host"] = System.Environment.MachineName;
    log4net.Config.XmlConfigurator.Configure();
}

The Application_Start function is called whenever your service is started. Also note the call to Configure to ensure log4net is initialized properly. You shouldn't have to modify anything else, well, besides your web.config of course.