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?

Shevek
  • 3,869
  • 5
  • 43
  • 63
  • 1
    For this particular property, you may not need to - "[by default the log4net:HostName property is set to the name of machine on which the event was originally logged](https://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html)" – stuartd Jun 15 '15 at 15:48
  • possible duplicate of [How do you log the machine name via log4net?](http://stackoverflow.com/questions/162810/how-do-you-log-the-machine-name-via-log4net) – Shevek Jun 15 '15 at 15:57
  • gah, that makes this a duplicate question! – Shevek Jun 15 '15 at 15:58
  • @Shevek: kinda, but your question also included having custom properties as well that might not be available by default in log4net, so... – shriek Jun 15 '15 at 16:01
  • I've expanded the question slightly to include other properties – Shevek Jun 15 '15 at 16:19

1 Answers1

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.

shriek
  • 5,157
  • 2
  • 36
  • 42