Friday, May 18, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: custom fields?
Prev Next
You are not authorized to post a reply.

Author Messages
Bill McCormick

10/02/2008 6:39 PM  
Hello,

Is it possible to add custom fields to log4net. I'd like to be able to do something like this:

log.InfoFormat("Custom1", "Custom2", "Custom3", "{0}","Message");

and then have an appender look something like:

<layout type="log4net.Layout.
PatternLayout">
  <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
</layout>


Thanks,

Bill
Georg Jansen

10/02/2008 6:58 PM  

Bill,

 

Add your custom columns to one of the properties collections, for example GlobalContext (or ThreadContext), for example:

 

log4net.GlobalContext.Properties["CustomCol1"] = "My custom column 1 value";

log4net.GlobalContext.Properties["CustomCol2"] = "My custom column 1 value";

 

Then pick up the value in the layout with the %property{propertyName} syntax, for example:

 

%property{CustomCol1}

 

BR,

Georg

http://www.l4ndash.com – Log4Net Dashboard

 

Francine Taylor

10/02/2008 7:01 PM  

Sure.  Here’s how you can create a custom dynamic field:

 

Put this in your application’s log4net setup:

 

// This is a way of creating a dynamic property (found in ActiveProperties.cs)

// To use it, put a reference to the property in the appender's property:  %property{custom1}

// It isn't actually used for anything right now, but I wanted it left in as an example

log4net.GlobalContext.Properties["custom1"] = new Custom1();

log4net.GlobalContext.Properties["custom2"] = new Custom2();

log4net.GlobalContext.Properties["custom2"] = new Custom3();

 

And then add this class to your project:

 

// You can use this class to create an active property to be used in the log4net configuration

//

// In your application setup routine you would create the property:

//     log4net.GlobalContext.Properties["sampler"] = new SampleProperty();

//

// Then in the log4net.xml file, a pattern might contain this reference:  %property{sampler}

//

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Genesis.Logging {

    public static class ActiveProperties {

        public static string custom1 = "custom1 value";

        public static string custom2 = "custom2 value";

        public static string custom3 = "custom3 value";

    }

 

    public class Custom1 {

        public override string ToString() {

            return ActiveProperties.custom1;

        }

    }

    public class Custom2 {

        public override string ToString() {

            return ActiveProperties.custom2;

        }

    }

 

    public class Custom3 {

        public override string ToString() {

            return ActiveProperties.custom3;

        }

    }

 

}

 

Then, in your InfoFormat method, just set the sampleProperty to whatever you passed in, just before you log the error.

Bill McCormick

10/02/2008 8:30 PM  
Thanks, so much Francine!!

If I understand correctly, once the GC property is set, it would stay set for every logged message thereafter? It would actually be better if it would be reset, since I would need (potentially) new values with each new logged message.

Thanks,


Bill
Michael Schall

10/02/2008 8:39 PM  
Take a look at the documentation around contexts and how they override each other...  Hopefully this will give you want you want...

We use both the GlobalContext and the ThreadContext and they work great.

Mike

 
Francine Taylor

10/02/2008 8:49 PM  

That’s why the dynamic setting is so useful.  If you were just going to set it to a value that doesn’t change, you would do it this way:

log4net.GlobalContext.Properties["custom1"] = "custom1 value";

But in the example that I gave, you are setting it not to a value, but to a class.  The ToString() value of the class, can be set dynamically, is what log4net uses to put the values into the %property.

 

So you can change those values at any time.  Using the class I gave you below, you might put this code in your logging method

 

public static void MyInfoLoggingMethod(string custom1, string custom2, string custom3, string message) {

    ActiveProperties.sampleProperty = custom1;

    ActiveProperties.sampleProperty = custom2;

    ActiveProperties.sampleProperty = custom3;

 

    _Log.Info(message);

}

 

Then when you log your message, those new values would be reflected.

 

Or am I not understanding what you need?

 

Francine Taylor

10/02/2008 8:56 PM  

Oops, sorry, didn’t quite convert everything completely from what I’ve got in my notes.  Should have been…

 

public static void MyInfoLoggingMethod(string custom1, string custom2, string custom3, string message) {

    ActiveProperties.custom1 = custom1;

    ActiveProperties.custom2 = custom2;

    ActiveProperties.custom3 = custom3;

 

    _Log.Info(message);

}

Bill McCormick

10/02/2008 8:58 PM  
No, you got it. I'd also need to make a separate wrapper class for for the Log.xFormat statements.

Thanks.
You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > custom fields?



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement