logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: logging additional message-specific data with AdoNetAppender
Date Sat, 10 May 2008 15:00:52 GMT
You can wrap ILog:

 ILogInfo log = new LogInfo(LogManager.GetLogger(GetType()));

You can also write your own LogManager that calls the "new LogInfo" code for you:

 ILogInfo log = Company.Logging.LogManager.GetLogger(GetType());

You can set your custom properties on the specific LoggingEvent object instead of on the thread
itself:

// ILogInfo implementation
public void Info(int eventId, object message, System.Exception t)
{
 if (this.IsInfoEnabled)
 {
  LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name,
Level.Info, message, t);
  loggingEvent.Properties["EventID"] = eventId;
  Logger.Log(loggingEvent);
 }
}

That code was taken from:

http://svn.apache.org/viewvc/logging/log4net/trunk/extensions/net/1.0/log4net.Ext.EventID/cs/src/EventIDLogImpl.cs?view=co

----- Original Message ----
From: Chris Jerdonek <jerdonek@granicus.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Saturday, May 10, 2008 10:51:12 AM
Subject: RE: logging additional message-specific data with AdoNetAppender

Thanks for your input, Peter and William.

What I'm leaning towards is making a simple wrapper of LogManager and
ILog so that when I call GetLogger, I get a logger that supports the
following syntax, where LogInfo is any class that implements an ILogInfo
interface:

log.Debug(new LogInfo(1001, "Friendly message...", "Stack trace..."));

ILogInfo has two methods Set and Unset which are meant to set and unset
ThreadContext.Properties, along with a Message property.  The logger's
Debug method, for example, is

public void Debug(ILogInfo info)
{
    info.Set();
    log.Debug(info.Message);
    info.Unset();
}

One nice thing is that I can easily make new ILogInfo classes as
necessary, or create more constructors for the LogInfo classes.
References to the ThreadContext.Properties string keys are also
restricted to the ILogInfo classes they pertain to.

--Chris




--
Chris Jerdonek
Software Developer
P: (415) 357-3618 Ext. 1357
Granicus, Inc.

-----Original Message-----
From: William R. Daniels, Jr. [mailto:bill@gmcps.com] 
Sent: Friday, May 09, 2008 3:00 PM
To: Log4NET User
Subject: RE: logging additional message-specific data with
AdoNetAppender



> -----Original Message-----
> From: Chris Jerdonek [mailto:jerdonek@granicus.com]
> Sent: Thursday, May 08, 2008 7:49 PM
> To: log4net-user@logging.apache.org
> Subject: logging additional message-specific data with AdoNetAppender
> 
> Hi, we've been using log4net for a few months now, and I've been on
this
> list for about the same.  I have a design question.

> To implement this, it seems natural to store these additional values
in
> ThreadContext.Properties, and then access them in the Web.config via
(for
> example) %property{msgcode}.
> 

> Any thoughts?
> 
> Okay, thanks,
> --Chris
> 

Chris,
We do exactly what you have indicated.  In our programs we set the extra
columns with ThreadContext and GlobalContext properties.  Works pretty
well for us.  Here's our config for the appender:


<appender name="ADONetAppender_SqlServer"
type="log4net.Appender.ADONetAppender">
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <!-- <levelMax value="FATAL" /> -->
      </filter>
      <param name="BufferSize" value="1" />
      <param name="ConnectionType"
value="System.Data.SqlClient.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <param name="ConnectionString"
value="server=SERVER;database=DATABASE;integrated security=SSPI;" />
      <param name="CommandText" value="INSERT INTO TABLE
([Date],[Thread],[Level],[Logger],[Message],[Exception],[Application_Cat
egory],[Application_Name],[Server_Name],[Carrier_Name],[DataBase_Name],[
Function_Name],[Message_Description]) VALUES (@log_date, @thread,
@log_level, @logger, @message, @exception, @category, @application,
@server, @carrier, @database, @function, @mesgdescription)" />
      <param name="Parameter">
        <param name="ParameterName" value="@log_date" />
        <param name="DbType" value="DateTime" />
        <param name="Layout" type="log4net.Layout.RawTimeStampLayout" />
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@thread" />
        <param name="DbType" value="String" />
        <param name="Size" value="255" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%t" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@log_level" />
        <param name="DbType" value="String" />
        <param name="Size" value="50" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%p" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@logger" />
        <param name="DbType" value="String" />
        <param name="Size" value="255" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%c" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@message" />
        <param name="DbType" value="String" />
        <param name="Size" value="4000" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%m" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@exception" />
        <param name="DbType" value="String" />
        <param name="Size" value="2000" />
        <param name="Layout" type="log4net.Layout.ExceptionLayout">
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@category" />
        <param name="DbType" value="String" />
        <param name="Size" value="100" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{category}" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@application" />
        <param name="DbType" value="String" />
        <param name="Size" value="100" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{application}" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@server" />
        <param name="DbType" value="String" />
        <param name="Size" value="50" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{log4net:HostName}"
/>
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@carrier" />
        <param name="DbType" value="String" />
        <param name="Size" value="100" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{carrier}" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@database" />
        <param name="DbType" value="String" />
        <param name="Size" value="100" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{database}" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@function" />
        <param name="DbType" value="String" />
        <param name="Size" value="100" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%P{function}" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@mesgdescription" />
        <param name="DbType" value="String" />
        <param name="Size" value="500" />
        <param name="Layout" type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%m" />
        </param>
      </param>
    </appender>

==============

Bill Daniels
Green Mountain Consulting, LLC

Mime
View raw message