Matthew,  

> Yes, I saw the config-examples on the apache site. What I 
> couldn't figure out is how to pass my own values into the 
> appender. For example, say I wanted to insert the error stack 
> into the db or application-specific identification numbers. 

The actual data that is passed as the prepared statement parameter comes
from a Layout object. The Layout is passed a LoggingEvent object and
extracts some information from the event. The most flexible built-in layout
is the PatternLayout that can extract most of the information from the
LoggingEvent and render it as a string. There is a
log4net.Layout.ExceptionLayout specifically designed to dump the exception
text into a parameter for the ADONetAppender, for example:

<parameter>
  <parameterName value="@exception" />
  <dbType value="String" />
  <size value="2000" />
  <layout type="log4net.Layout.ExceptionLayout" />
</parameter>

If none of the built-in Layouts meet your requirements then you can write
your own. Just implement the log4net.Layout.ILayout interface. In fact the
ADONetAppender supports using the log4net.Layout.IRawLayout interface which
allows passing database types rather than just strings. When writing a
custom Layout the biggest challenge is usually deciding where to get the
data from if it is not part of the data captured in the LoggingEvent.

If you just need to pass a fixed string as a parameter you can set this
using the PatternLayout as follows:

<parameter>
  <parameterName value="@appName" />
  <dbType value="String" />
  <size value="255" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="MyApplication" />
  </layout>
</parameter>

 
> Also, how would I go about constructing the logging appenders 
> programmatically? Which objects would I have to inherit from? 
> I could not find any examples of this in the documentation. 
> Is there any sample code to look at? 

You can create an ADONetAppender by doing something like this:

public ADONetAppender CreateADONetAppender() 
{
  ADONetAppender ado = new ADONetAppender();
  ado.BufferSize = 1;

  //connection stuff
  ado.CommandType = System.Data.CommandType.StoredProcedure;
  ado.CommandText = "InsertLogEntry";
  ado.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
  ado.ConnectionString = "..."; //omitted
                        
  //parameters
  ADONetAppenderParameter param = new ADONetAppenderParameter();
  param.ParameterName = "@message";
  param.DbType = System.Data.DbType.String;
  param.Size = 2000;
  param.Layout = new Layout2RawLayoutAdapter(new
log4net.Layout.PatternLayout("%m"));
  ado.AddParameter(param);
        
  param = new ADONetAppenderParameter();
  param.ParameterName = "@exception";
  param.DbType = System.Data.DbType.String;
  param.Size = 2000;
  param.Layout = new Layout2RawLayoutAdapter(new
log4net.Layout.ExceptionLayout());
  ado.AddParameter(param);                              

  ado.ActivateOptions();
  return ado;
}


You can programmatically configure log4net by doing something like this in
the start-up sequence:

BasicConfigurator.Configure(CreateADONetAppender());


If you need more control over the configuration then you can write the more
verbose:

log4net.Repository.Hierarchy.Hierarchy hierarchy = null;
log4net.Repository.Hierarchy.Logger logger = null;
hierarchy =
(log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();

// Configure root logger
logger = hierarchy.Root;        
logger.Level = Level.Debug;
logger.AddAppender(CreateADONetAppender());

// Configure named logger
logger =
(log4net.Repository.Hierarchy.Logger)hierarchy.GetLogger("mylogger");
logger.Level = Level.Warn;

hierarchy.Configured = true;


Hope that helps,

Nicko


> 
> Thank you.
> 
>               -----Original Message-----
>               From: Nicko Cadell <[EMAIL PROTECTED]>@DUKEPOWER
>               Sent: May 16, 2004 1:00 PM
>               To: 'Log4NET User'
>               Subject: RE: questions
> 
>               Matthew,
> 
>               > *       How are parameters added to the ADONetAppender
> 
>               > (Exception's stack trace, hostname, other 
> exception or custom
> 
>               > information)? How are "dynamic" parameters 
> passed to the appender?
> 
>               There are some examples of configuring the 
> ADONetAppender on this page:
> 
>               
> http://logging.apache.org/log4net/release/config-examples.html
> 
>               Each parameter is a ADONetAppenderParameter 
> object passed to the
> 
>               ADONetAppender's AddParameter() method. The 
> ADONetAppenderParameter
> 
>               specifies the stored procedure or prepared 
> statement parameter name, the
> 
>               database datatype, etc... and a Layout object 
> which is used to render the
> 
>               value of the parameter from the LoggingEvent object.
> 
> 
>               > *       How to lock a standard set of appenders for a
> 
>               > development framework? Should a standard 
> config file be used
> 
>               > or a custom component written? How difficult 
> would is it to
> 
>               > implement a component that uses a standard 
> set of appenders
> 
>               > and configurations?
> 
>               If you need to embed a configuration for 
> log4net you can either construct
> 
>               the logging appenders etc.. programmatically or 
> you can embed the
> 
>               configuration file as a resource and then use the
> 
>               DOMConfigurator.Configure(Stream) method to 
> load the configuration with the
> 
>               stream to the embedded resource.
> 
> 
>               > *       What is NDC/MDC? What is the 
> significance of NDC and
> 
>               > MDC? And, why should I implement it?
> 
>               The NDC and MDC allow the developer to capture 
> contextual information that
> 
>               is then included in subsequent log messages. 
> For further details see:
> 
>               
> http://logging.apache.org/log4net/release/manual/contexts.html
> 
> 
>               Cheers,
> 
>               Nicko
> 
> 

Reply via email to