4

I am able to set up log4NET with the ADONetAppender, and all works fine when I want to capture the state of things when I log a message via log.Info(message).

As I will be logging from various locations in my application based on an ActionID which changes throughout the application, how do I extend things so that I may instead issue a call such as log.Info(ActionID, message), with ActionID ending up in the database?

John Smith
  • 165
  • 2
  • 10

3 Answers3

7

You can add custom fields to log4net using GlobalContext.Properties before calling log.Info() by doing something like this:

GlobalContext.Properties["ActionID"] = ActionID;

Then, in your ADONetAppender configuration, you can access this custom field with %property{ActionID}.

Brandon Wood
  • 5,347
  • 4
  • 38
  • 31
4

I think that @bcwood probably has the best/easiest idea to simply use GlobalContext.Properties to store the id that you want to log.

However, if the id is very important to you and if you want to streamline the setting of the id (such as by adding a parameter to the various logging methods vs having to add a separate call to set the id value in the GlobalContext), then you might have a look at this folder in the log4net repository.

http://svn.apache.org/viewvc/logging/log4net/trunk/extensions/net/1.0/log4net.Ext.EventID/cs/src/

It contains an example of how to extend the log4net logger to add an "EventID" parameter.

It seems pretty involved to me (subclass the log4net logger, create your own LogManager to dispense your loggers, etc).

You can probably go a little bit simpler if you choose to wrap log4net, and use log4net's logger internally to log.

The advantage of using the approach that I linked to above (or a similar, but simpler approach of wrapping log4net's Logger), is that your logging call sites can look like you proposed:

logger.Info(123, "Hello");
logger.Info(321, "Good bye");

As compared to @bcwood's suggestion, which would make your logging call sites look like this:

GlobalContext.Properties["ActionID"] = 123;
logger.Info("Hello");
GlobalContext.Properties["ActionID"] = 321;
logger.Info("Good bye");
wageoghe
  • 27,390
  • 13
  • 88
  • 116
0

Try creating a custom function by wrapping the info method.

void InfoLog(int ActionID, string message)
{
   log.info(String.Format("{0}:{1}",ActionID.ToString(),message));
}
Sunny
  • 4,765
  • 5
  • 37
  • 72
  • will this make the ActionID parameter accessible to the ADONetAppender? Ultimately, ActionID needs to end up in it's own database field. – John Smith Dec 05 '12 at 20:27
  • No, it just appends the actionid to the message being logged. – Sunny Dec 05 '12 at 20:29
  • I need something like the following: log.info(5,"my message") to store 5 in the statusID field of my database, and "my message" in the message field of the database. I already having things working in that log.info(message) and related information is tracked, but I need log.info(value,message) to post value to the db. Is this possible within log4net. – John Smith Dec 05 '12 at 20:35
  • 1
    check out similar requirement http://stackoverflow.com/questions/12139486/log4net-how-to-add-a-custom-field-to-my-logging – Sunny Dec 05 '12 at 20:51