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 puska <mr...@gmail.com> on 2006/08/04 14:32:43 UTC

Best way for inserting additional message in log?

Hi

What I'm trying to do is to include new field (column) in Sql DB, with some
additional business related information.
I need this optional info (parameter) in NEW column for fastest browsing and
searching.
Idea is to call log from application with new parameter:
log.Info("My message","Some business data");
// or log.Info("My message", ex, "Some business data"); // with Exception

I'm already using custom wrapper for log4net, so I easily overloaded Info
function:
public void Info(object message, Exception ex, object additional)
    {
        using (log4net.LogicalThreadContext.Stacks["additional"].Push(
additional.ToString()))
        {
            this.Logger.Logger.Log(
                  _declaringType,
                  log4net.Core.Level.Info,
                  message,
                  ex);
             }
    }

and I put new parameter in SQLAdoAppender (in config file):
      <parameter>
        <parameterName value="@Additional" />
        <dbType value="String" />
        <size value="1024" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property" />
        </layout>
      </parameter>

and this is working (almost) fine.

My questions are:
1. Is there any better (fastest) way for doing this?
2. My additional parameter is strictly related to called logging event.
Would I have on this way interference with other logs?
3. How to get only my additional info from property? I tried this, but it's
not working:
          <conversionPattern value="%property{additional}" />
4. when I make some error in configuration file (like one above) and then
correct it, logging won't work until I restart my web application?!

Thanks, Mario


PS I'm using log4net-1.2.10 in my ASP.NET 2.0 application

Re: Best way for inserting additional message in log?

Posted by puska <mr...@gmail.com>.
> (1)
> public void Info(object message, Exception ex, object additional)
> {
>  if (Logger.IsEnabledFor(Level.Info))
>  {
>   LoggingEvent loggingEvent = new LoggingEvent(
>    declaringType,
>    Logger.Repository,
>    Logger.Name,
>    Level.Info,
>    message,
>    ex);
>
>   loggingEvent.Properties["additional"] = additional;
>
>   Logger.Log(loggingEvent);
>  }
> }

I just did same thing few days ago :)  (thx to log4net.Ext.EventID)
It works like charm.


> (3)
> If the call looks like this:
>  log.Info("Oh no!", ex, 1234);
> I can access 1234 by using the property pattern:
>  <conversionPattern value="%property{additional}" />

yes, yes, I know it should work like this.... and it does, but ( this
is interesting):
When I used Integrated Security in my connection string and with
<conversionPattern value="%property" />
everything is fine (because log4net impersonates 'me' when connecting
to database)
and when I'm using
<conversionPattern value="%property{additional}" />
it stops logging (because log4net tries to connect to db as ASPNET,
and I naturally forgot to give ASPNET rights for db)

I simply solved this by using user and psw in connection string (this
is what I intended to do from beginning).


> (4)
> If you configure log4net using the ConfigureAndWatch method ...
> log4net will attempt to reload itself when the configuration file
> changes.

I was using ConfigureAndWatch and this is still problem for me.
When I make any changes in log4net.config they are reloaded instantly,
but when I make same error in log4net.config, logging won't work. And if I then
correct this error logging still won't work until I restart my web application.


> --- puska <mr...@gmail.com> wrote:
>
> > Hi
> >
> > What I'm trying to do is to include new field (column) in Sql DB,
> > with some
> > additional business related information.
> > I need this optional info (parameter) in NEW column for fastest
> > browsing and
> > searching.
> > Idea is to call log from application with new parameter:
> > log.Info("My message","Some business data");
> > // or log.Info("My message", ex, "Some business data"); // with
> > Exception
> >
> > I'm already using custom wrapper for log4net, so I easily overloaded
> > Info
> > function:
> > public void Info(object message, Exception ex, object additional)
> >     {
> >         using
> > (log4net.LogicalThreadContext.Stacks["additional"].Push(
> > additional.ToString()))
> >         {
> >             this.Logger.Logger.Log(
> >                   _declaringType,
> >                   log4net.Core.Level.Info,
> >                   message,
> >                   ex);
> >              }
> >     }
> >
> > and I put new parameter in SQLAdoAppender (in config file):
> >       <parameter>
> >         <parameterName value="@Additional" />
> >         <dbType value="String" />
> >         <size value="1024" />
> >         <layout type="log4net.Layout.PatternLayout">
> >           <conversionPattern value="%property" />
> >         </layout>
> >       </parameter>
> >
> > and this is working (almost) fine.
> >
> > My questions are:
> > 1. Is there any better (fastest) way for doing this?
> > 2. My additional parameter is strictly related to called logging
> > event.
> > Would I have on this way interference with other logs?
> > 3. How to get only my additional info from property? I tried this,
> > but it's
> > not working:
> >           <conversionPattern value="%property{additional}" />
> > 4. when I make some error in configuration file (like one above) and
> > then
> > correct it, logging won't work until I restart my web application?!
> >
> > Thanks, Mario
> >
> >
> > PS I'm using log4net-1.2.10 in my ASP.NET 2.0 application
> >
>
>

Re: Best way for inserting additional message in log?

Posted by Ron Grabowski <ro...@yahoo.com>.
(1)
Here's another way of storing extra information with the logging event:

// untested
public void Info(object message, Exception ex, object additional)
{
 if (Logger.IsEnabledFor(Level.Info))
 {
  LoggingEvent loggingEvent = new LoggingEvent(
   declaringType,
   Logger.Repository,
   Logger.Name,
   Level.Info,
   message,
   ex);
 
  loggingEvent.Properties["additional"] = additional;
 
  Logger.Log(loggingEvent);
 }
}

(2)
(3)
If the call looks like this:

 log.Info("Oh no!", ex, 1234);

I can access 1234 by using the property pattern:

 <conversionPattern value="%property{additional}" />

(4)
If you configure log4net using the ConfigureAndWatch method:

 XmlConfigurator.ConfigureAndWatch(
  new FileInfo(
  AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
  "log4net.config"));

log4net will attempt to reload itself when the configuration file
changes.

--- puska <mr...@gmail.com> wrote:

> Hi
> 
> What I'm trying to do is to include new field (column) in Sql DB,
> with some
> additional business related information.
> I need this optional info (parameter) in NEW column for fastest
> browsing and
> searching.
> Idea is to call log from application with new parameter:
> log.Info("My message","Some business data");
> // or log.Info("My message", ex, "Some business data"); // with
> Exception
> 
> I'm already using custom wrapper for log4net, so I easily overloaded
> Info
> function:
> public void Info(object message, Exception ex, object additional)
>     {
>         using
> (log4net.LogicalThreadContext.Stacks["additional"].Push(
> additional.ToString()))
>         {
>             this.Logger.Logger.Log(
>                   _declaringType,
>                   log4net.Core.Level.Info,
>                   message,
>                   ex);
>              }
>     }
> 
> and I put new parameter in SQLAdoAppender (in config file):
>       <parameter>
>         <parameterName value="@Additional" />
>         <dbType value="String" />
>         <size value="1024" />
>         <layout type="log4net.Layout.PatternLayout">
>           <conversionPattern value="%property" />
>         </layout>
>       </parameter>
> 
> and this is working (almost) fine.
> 
> My questions are:
> 1. Is there any better (fastest) way for doing this?
> 2. My additional parameter is strictly related to called logging
> event.
> Would I have on this way interference with other logs?
> 3. How to get only my additional info from property? I tried this,
> but it's
> not working:
>           <conversionPattern value="%property{additional}" />
> 4. when I make some error in configuration file (like one above) and
> then
> correct it, logging won't work until I restart my web application?!
> 
> Thanks, Mario
> 
> 
> PS I'm using log4net-1.2.10 in my ASP.NET 2.0 application
>