2

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?

5

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! For Unity.WCF it actually goes in the Factory class but it works!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.