You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Weston Weems <ww...@gmail.com> on 2005/06/03 01:16:24 UTC

Advanced Configuration

I've just begun to play with log4net after trying endlessly to get
Enterprise Library working as I'd like it.

I do have a few questions I was unable to find the answers for on my
own (reading documentation/manual on logging.apache.org)

this is all .net 1.1 and using 1.9b or whatever the latest build was.

1) is there a way to explicitly from code tell my log entry to use a
specific appender?

2) For adoappender, is there way to provide more properties of a log
entry, and log that into more columns etc? Eg, if I wanted to include
username, version of windows etc.

3) from config file, is there somethinga little more finegrained for
routing error messages than levels and string matches? I'd like to be
able to "flag" an error message and if the config permits, have it
processed with a specific appender. (I realize I could use the filter
and filter on a string I throw in there, just seems cumbersome)


Anyway thank you guys very much
Weston Weems

Re: Advanced Configuration

Posted by Ron Grabowski <ro...@yahoo.com>.
How would you pass in a non-string into the conversion pattern?

 <conversionPattern value="???" />

--- Weston Weems <ww...@gmail.com> wrote:

> No... I honestly didnt have anything in mind specifically... just
> didnt know if the conversion would just take string type values...
> 
> I guess its not a big deal at all.. just curious
> 
> On 6/2/05, Ron Grabowski <ro...@yahoo.com> wrote:
> > I don't understand your final question. What are you trying to log?
> > 
> >  "just get to use simple types"
> > 
> > Are you wanting to log the output from something like a
> > BinaryFormatter?
> > 
> > --- Weston Weems <ww...@gmail.com> wrote:
> > 
> > > Well my main goals are:
> > >
> > > log additional info (totally understand how to do that one)
> > >
> > > explicitly log to email etc from code (I guess I could just get a
> > > logger thats designed to do that and only that...
> > >
> > > and I can prolly just filter on log text, so that stuff thats
> > > database
> > > related might be emailed, where a simple error may not be
> > >
> > >
> > > One last question, with the conversion pattern, do you just get
> to
> > > use
> > > simple types?
> >
> 


Re: Advanced Configuration

Posted by Weston Weems <ww...@gmail.com>.
No... I honestly didnt have anything in mind specifically... just
didnt know if the conversion would just take string type values...

I guess its not a big deal at all.. just curious

On 6/2/05, Ron Grabowski <ro...@yahoo.com> wrote:
> I don't understand your final question. What are you trying to log?
> 
>  "just get to use simple types"
> 
> Are you wanting to log the output from something like a
> BinaryFormatter?
> 
> --- Weston Weems <ww...@gmail.com> wrote:
> 
> > Well my main goals are:
> >
> > log additional info (totally understand how to do that one)
> >
> > explicitly log to email etc from code (I guess I could just get a
> > logger thats designed to do that and only that...
> >
> > and I can prolly just filter on log text, so that stuff thats
> > database
> > related might be emailed, where a simple error may not be
> >
> >
> > One last question, with the conversion pattern, do you just get to
> > use
> > simple types?
>

Re: Advanced Configuration

Posted by Ron Grabowski <ro...@yahoo.com>.
I don't understand your final question. What are you trying to log?

 "just get to use simple types" 

Are you wanting to log the output from something like a
BinaryFormatter?

--- Weston Weems <ww...@gmail.com> wrote:

> Well my main goals are:
> 
> log additional info (totally understand how to do that one)
> 
> explicitly log to email etc from code (I guess I could just get a
> logger thats designed to do that and only that...
> 
> and I can prolly just filter on log text, so that stuff thats
> database
> related might be emailed, where a simple error may not be
> 
> 
> One last question, with the conversion pattern, do you just get to
> use
> simple types?

Re: Advanced Configuration

Posted by Weston Weems <ww...@gmail.com>.
Well my main goals are:

log additional info (totally understand how to do that one)

explicitly log to email etc from code (I guess I could just get a
logger thats designed to do that and only that...

and I can prolly just filter on log text, so that stuff thats database
related might be emailed, where a simple error may not be


One last question, with the conversion pattern, do you just get to use
simple types?

On 6/2/05, Ron Grabowski <ro...@yahoo.com> wrote:
> Log statements are sent to a Logger which in turn writes to one or more
> Appenders. First you must get a Logger. Most people use the class name
> as the name of the logger:
> 
>  namespace Company.Project
>  {
>   public class Foo
>   {
>     ILog log = LogManager.GetLogger(typeof(Foo));
> 
>     public void TestMethod()
>     {
>      log.Debug("Hello World");
>     }
>   }
>  }
> 
> Then in your log4net file you decide what Appenders the Logger should
> write to. The text below will write output to the MyFileAppender
> Appender:
> 
>  <logger name="Company.Project.Foo">
>   <appender-ref ref="MyFileAppender" />
>  </logger>
> 
> MyFileAppender is defined as:
> 
>  <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
>   <file value="c:\log.txt">
>   <appendToFile value="false" />
>   <layout type="log4net.Layout.SimpleLayout" />
>  </appender>
> 
> You should see the following line in your output file:
> 
>  DEBUG - Hello World
> 
> People usually include much more information in their log entries. For
> example, this is what I use for a FileAppenders:
> 
>  <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
>   <file value="c:\log.txt">
>   <appendToFile value="false" />
>   <layout type="log4net.Layout.PatternLayout,log4net">
>    <conversionPattern value="%5p %d{yyyy-MM-dd hh:mm:ss tt} (%c:%L) -
> %m%n" />
>   </layout>
>  </appender>
> 
> That conversionPattern will record a log statement with information
> such as the log level, time of the log event, the class, the line
> number where the log originated from, and my message:
> 
>  INFO 2005-06-02 05:53:55 PM (Company.Project.Global:85) - Application
> started
> 
> A very common error when people first start using log4net is that they
> have not made the destination directory for log files writtable.
> 
> The <logger> node I used above will only snag log messages generated by
> the Company.Project.Foo class. If you want to log message from other
> classes or from Loggers that weren't created from classes:
> 
>  ILog auditLog = LogManager.GetLog("AUDIT");
> 
> You can use the more general root logger:
> 
>  <root>
>   <appender-ref ref="MyFileAppender" />
>  </root>
> 
> Every log4net config file requires there be a root logger. You may
> define additional <logger> nodes as necessary. The example below will
> write logs belonging to the "AUDIT" Logger to one destination and logs
> belonging to Company.Project.Foo to another destination.
> 
>  <!-- required -->
>  <root>
>   <appender-ref ref="AuditFileAppender" />
>  </root>
> 
>  <!-- optional -->
>  <logger name="Company.Project.Foo">
>   <appender-ref ref="MyFileAppender" />
>  </logger>
> 
> Its also possible to write the output from a Logger to more than one
> destination:
> 
>  <root>
>   <appender-ref ref="MyFileAppender" />
>  </root>
> 
>  <logger name="AUDIT">
>   <appender-ref ref="MyEmailAppender" />
>   <appender-ref ref="DatabaseAppender" />
>  </logger>
> 
> Suppose I have other classes in the Company.Project namespace: Bar,
> Baz, and Boo. I can use this syntax to capture log statements from all
> 4 classes:
> 
>  <logger name="Company.Project">
>   <appender-ref ref="MyFileAppender" />
>  </logger>
> 
> To answer your second question, you can store additional pieces of
> information that you want to appear on the log messages by using the
> ThreadContext:
> 
>  log4net.ThreadContext.Properties["UserId"] = userId;
>  log4net.ThreadContext.Properties["WindowsVersion"] = windowsVersion;
> 
>  log.Debug("Hello World");
>  log.Debug("World Hello");
> 
> To reference these values in your appender you would do this:
> 
>  <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
>   <file value="c:\log.txt">
>   <appendToFile value="false" />
>   <layout type="log4net.Layout.PatternLayout,log4net">
>    <conversionPattern value="%p %d %property{UserId} - %m%n" />
>   </layout>
>  </appender>
> 
> Log4net already comes with many pre-defined patterns:
> 
> http://tinyurl.com/e3nd3
> http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html
> 
> Its easy to define custom patterns:
> 
>  <conversionPattern value="%p %d %querystring{ProductId} - %m%n" />
>  <conversionPattern value="%p %d %foo{Bar} - %m%n" />
>  <conversionPattern value="%p %d %minutes-till-lunch - %m%n" />
> 
> How do you intend to "flag" your message when you write it to the
> Logger?
> 
>  log.DebugFormat("This message is flagged: {0}", ???);
> 
> Its possible to add new log levels to log4net:
> 
>  log.Flag("This is a special message.");
> 
> You could setup a special Appender that only records "flagged"
> messages.
> 
> All of the above examples involving xml could be translated to code.
> Its possible to deploy log4net without a config file.
> 
> --- Weston Weems <ww...@gmail.com> wrote:
> 
> > I've just begun to play with log4net after trying endlessly to get
> > Enterprise Library working as I'd like it.
> >
> > I do have a few questions I was unable to find the answers for on my
> > own (reading documentation/manual on logging.apache.org)
> >
> > this is all .net 1.1 and using 1.9b or whatever the latest build was.
> >
> > 1) is there a way to explicitly from code tell my log entry to use a
> > specific appender?
> >
> > 2) For adoappender, is there way to provide more properties of a log
> > entry, and log that into more columns etc? Eg, if I wanted to include
> > username, version of windows etc.
> >
> > 3) from config file, is there somethinga little more finegrained for
> > routing error messages than levels and string matches? I'd like to be
> > able to "flag" an error message and if the config permits, have it
> > processed with a specific appender. (I realize I could use the filter
> > and filter on a string I throw in there, just seems cumbersome)
> >
> >
> > Anyway thank you guys very much
> > Weston Weems
> >
> 
>

Re: Advanced Configuration

Posted by Ron Grabowski <ro...@yahoo.com>.
Log statements are sent to a Logger which in turn writes to one or more
Appenders. First you must get a Logger. Most people use the class name
as the name of the logger:

 namespace Company.Project
 {
  public class Foo
  {
    ILog log = LogManager.GetLogger(typeof(Foo));
    
    public void TestMethod()
    {
     log.Debug("Hello World");
    }
  }
 }

Then in your log4net file you decide what Appenders the Logger should
write to. The text below will write output to the MyFileAppender
Appender:

 <logger name="Company.Project.Foo">
  <appender-ref ref="MyFileAppender" />
 </logger>

MyFileAppender is defined as:

 <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
  <file value="c:\log.txt">
  <appendToFile value="false" />
  <layout type="log4net.Layout.SimpleLayout" />
 </appender>

You should see the following line in your output file:

 DEBUG - Hello World

People usually include much more information in their log entries. For
example, this is what I use for a FileAppenders:

 <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
  <file value="c:\log.txt">
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout,log4net">
   <conversionPattern value="%5p %d{yyyy-MM-dd hh:mm:ss tt} (%c:%L) -
%m%n" />
  </layout>
 </appender>

That conversionPattern will record a log statement with information
such as the log level, time of the log event, the class, the line
number where the log originated from, and my message:

 INFO 2005-06-02 05:53:55 PM (Company.Project.Global:85) - Application
started

A very common error when people first start using log4net is that they
have not made the destination directory for log files writtable.

The <logger> node I used above will only snag log messages generated by
the Company.Project.Foo class. If you want to log message from other
classes or from Loggers that weren't created from classes:

 ILog auditLog = LogManager.GetLog("AUDIT");

You can use the more general root logger:

 <root>
  <appender-ref ref="MyFileAppender" />
 </root>

Every log4net config file requires there be a root logger. You may
define additional <logger> nodes as necessary. The example below will
write logs belonging to the "AUDIT" Logger to one destination and logs
belonging to Company.Project.Foo to another destination.

 <!-- required -->
 <root>
  <appender-ref ref="AuditFileAppender" />
 </root>

 <!-- optional -->
 <logger name="Company.Project.Foo">
  <appender-ref ref="MyFileAppender" />
 </logger>

Its also possible to write the output from a Logger to more than one
destination:

 <root>
  <appender-ref ref="MyFileAppender" />
 </root>

 <logger name="AUDIT">
  <appender-ref ref="MyEmailAppender" />
  <appender-ref ref="DatabaseAppender" />
 </logger>

Suppose I have other classes in the Company.Project namespace: Bar,
Baz, and Boo. I can use this syntax to capture log statements from all
4 classes:

 <logger name="Company.Project">
  <appender-ref ref="MyFileAppender" />
 </logger>

To answer your second question, you can store additional pieces of
information that you want to appear on the log messages by using the
ThreadContext:

 log4net.ThreadContext.Properties["UserId"] = userId;
 log4net.ThreadContext.Properties["WindowsVersion"] = windowsVersion;

 log.Debug("Hello World");
 log.Debug("World Hello");

To reference these values in your appender you would do this:

 <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
  <file value="c:\log.txt">
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout,log4net">
   <conversionPattern value="%p %d %property{UserId} - %m%n" />
  </layout>
 </appender>

Log4net already comes with many pre-defined patterns:

http://tinyurl.com/e3nd3
http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

Its easy to define custom patterns:

 <conversionPattern value="%p %d %querystring{ProductId} - %m%n" />
 <conversionPattern value="%p %d %foo{Bar} - %m%n" />
 <conversionPattern value="%p %d %minutes-till-lunch - %m%n" />

How do you intend to "flag" your message when you write it to the
Logger?

 log.DebugFormat("This message is flagged: {0}", ???);

Its possible to add new log levels to log4net:

 log.Flag("This is a special message.");

You could setup a special Appender that only records "flagged"
messages.

All of the above examples involving xml could be translated to code.
Its possible to deploy log4net without a config file.

--- Weston Weems <ww...@gmail.com> wrote:

> I've just begun to play with log4net after trying endlessly to get
> Enterprise Library working as I'd like it.
> 
> I do have a few questions I was unable to find the answers for on my
> own (reading documentation/manual on logging.apache.org)
> 
> this is all .net 1.1 and using 1.9b or whatever the latest build was.
> 
> 1) is there a way to explicitly from code tell my log entry to use a
> specific appender?
> 
> 2) For adoappender, is there way to provide more properties of a log
> entry, and log that into more columns etc? Eg, if I wanted to include
> username, version of windows etc.
> 
> 3) from config file, is there somethinga little more finegrained for
> routing error messages than levels and string matches? I'd like to be
> able to "flag" an error message and if the config permits, have it
> processed with a specific appender. (I realize I could use the filter
> and filter on a string I throw in there, just seems cumbersome)
> 
> 
> Anyway thank you guys very much
> Weston Weems
>