Discussion:
Building A Custom Logger
Kevin Parkinson
2007-07-17 23:03:59 UTC
Permalink
Has anyone had experience building a custom logger?

Here is what I am doing. I have created my own interface describing what the
kinds of things a logger needs to be able to log in my environment (let's
call it IInterface) like security audits, performance statistics, etc. This
interface extends ILog as well.
Thus: IInterface : ILog

Then, I have an implementation of IInterface and we'll call that
InterfaceImpl which also inherits from LogImpl (in order to satisfy the ILog
interface).
Like so: InterfaceImpl : LogImpl, IInterface

Now, when I go to put a logger in a class in which I want to perform
logging, by default I would usually put the line of code:
ILog _logger = LogManager.GetLogger(typeof(MyClass));

What I would like to do is to be able to say:
IInterface _logger = LogManager.GetLogger(typeof(MyClass));

...but this does not work (nor does anything else I have tried) because
GetLogger returns LogImpl which knows nothing of my IInterface.

Can anyone recommend anything that I can try without needing to modify the
LogManager (a sealed class)?
Jon Finley
2007-07-17 23:13:11 UTC
Permalink
Hi Kevin,



I am not a log4net guru so hopefully one of the experienced guys will chime
in. In the mean time.



I have created a customer logger and use the following statement when adding
the logger to a class:



Private Shared mLog As IGSIEventLog =
GSIEventLogManager.GetLogger((Reflection.MethodBase.GetCurrentMethod().Decla
ringType))



The big difference is referencing "my" log manager and not the "default"
log4net log manager.



Also, I created my logger in the log4net.Ext.GSI namespace and so have the
following imports:



Imports log4net.Ext.GSI



Hope that helps!



Jon





From: Kevin Parkinson [mailto:***@gmail.com]
Sent: Tuesday, July 17, 2007 5:04 PM
To: log4net-***@logging.apache.org
Subject: Building A Custom Logger



Has anyone had experience building a custom logger?

Here is what I am doing. I have created my own interface describing what the
kinds of things a logger needs to be able to log in my environment (let's
call it IInterface) like security audits, performance statistics, etc. This
interface extends ILog as well.
Thus: IInterface : ILog

Then, I have an implementation of IInterface and we'll call that
InterfaceImpl which also inherits from LogImpl (in order to satisfy the ILog
interface).
Like so: InterfaceImpl : LogImpl, IInterface

Now, when I go to put a logger in a class in which I want to perform
logging, by default I would usually put the line of code:
ILog _logger = LogManager.GetLogger(typeof(MyClass));

What I would like to do is to be able to say:
IInterface _logger = LogManager.GetLogger(typeof(MyClass));

...but this does not work (nor does anything else I have tried) because
GetLogger returns LogImpl which knows nothing of my IInterface.

Can anyone recommend anything that I can try without needing to modify the
LogManager (a sealed class)?
Kevin Parkinson
2007-07-18 00:19:29 UTC
Permalink
Jon:

Thanks for the reply!

I had thought of doing what you have done but I was wondering if there is an
easier way.

When you built your own logger, was it a big job, i.e. did you find yourself
having to implement a whole bunch the methods already on the default
LogManager? were there any other classes/interfaces that you had to
build/extend/implement to make this work?

Kevin Parkinson
Post by Jon Finley
Hi Kevin,
I am not a log4net guru so hopefully one of the experienced guys will
chime in. In the mean timeĀ…
I have created a customer logger and use the following statement when
Private Shared mLog As IGSIEventLog = GSIEventLogManager.GetLogger((
Reflection.MethodBase.GetCurrentMethod().DeclaringType))
The big difference is referencing "my" log manager and not the "default"
log4net log manager.
Also, I created my logger in the log4net.Ext.GSI namespace and so have the
Imports log4net.Ext.GSI
Hope that helps!
Jon
*Sent:* Tuesday, July 17, 2007 5:04 PM
*Subject:* Building A Custom Logger
Has anyone had experience building a custom logger?
Here is what I am doing. I have created my own interface describing what
the kinds of things a logger needs to be able to log in my environment
(let's call it IInterface) like security audits, performance statistics,
etc. This interface extends ILog as well.
Thus: IInterface : ILog
Then, I have an implementation of IInterface and we'll call that
InterfaceImpl which also inherits from LogImpl (in order to satisfy the ILog
interface).
Like so: InterfaceImpl : LogImpl, IInterface
Now, when I go to put a logger in a class in which I want to perform
ILog _logger = LogManager.GetLogger(typeof(MyClass));
IInterface _logger = LogManager.GetLogger(typeof(MyClass));
...but this does not work (nor does anything else I have tried) because
GetLogger returns LogImpl which knows nothing of my IInterface.
Can anyone recommend anything that I can try *without* needing to modify
the LogManager (a sealed class)?
Vanderkolk, John
2007-07-18 12:35:40 UTC
Permalink
The cleanest way of doing this would be to implement your own
LogManager. However if you really don't want to do that and want to just
have your IInterface and InterfaceImpl, the best way I see would be to
make sure your InterfaceImpl constructor is public and use log4net's
LogManager to get an ILog, and use its ILogger to initialize your
InterfaceImpl:

IInterface _logger = new
InterfaceImpl(log4net.LogManager(typeof(MyClass)).Logger);

A cleaner way would be to add a new constructor to your InterfaceImpl:

public InterfaceImpl(Type type) : base
(log4net.LogManager.GetLogger(type).Logger) { }

so that you can do this:

IInterface _logger = new InterfaceImpl(typeof(MyClass));


John VanderKolk




The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.


From: Kevin Parkinson [mailto:***@gmail.com]
Sent: Tuesday, July 17, 2007 7:04 PM
To: log4net-***@logging.apache.org
Subject: Building A Custom Logger


Has anyone had experience building a custom logger?

Here is what I am doing. I have created my own interface describing what
the kinds of things a logger needs to be able to log in my environment
(let's call it IInterface) like security audits, performance statistics,
etc. This interface extends ILog as well.
Thus: IInterface : ILog

Then, I have an implementation of IInterface and we'll call that
InterfaceImpl which also inherits from LogImpl (in order to satisfy the
ILog interface).
Like so: InterfaceImpl : LogImpl, IInterface

Now, when I go to put a logger in a class in which I want to perform
logging, by default I would usually put the line of code:
ILog _logger = LogManager.GetLogger(typeof(MyClass));

What I would like to do is to be able to say:
IInterface _logger = LogManager.GetLogger(typeof(MyClass));

...but this does not work (nor does anything else I have tried) because
GetLogger returns LogImpl which knows nothing of my IInterface.

Can anyone recommend anything that I can try without needing to modify
the LogManager (a sealed class)?
Kevin Parkinson
2007-07-18 15:47:30 UTC
Permalink
Bless you, John Vanderkolk!

I have chosen to use:

public InterfaceImpl(Type type) : base
(log4net.LogManager.GetLogger(type).Logger)
{ }

IInterface _logger = new InterfaceImpl(typeof(MyClass));

...this makes a lot of sense and it works!
The cleanest way of doing this would be to implement your own LogManager.
However if you really don't want to do that and want to just have your
IInterface and InterfaceImpl, the best way I see would be to make sure your
InterfaceImpl constructor is public and use log4net's LogManager to get an
IInterface _logger = new InterfaceImpl(log4net.LogManager
(typeof(MyClass)).Logger);
public InterfaceImpl(Type type) : base (log4net.LogManager.GetLogger(type).Logger)
{ }
IInterface _logger = new InterfaceImpl(typeof(MyClass));
John VanderKolk
The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.
*Sent:* Tuesday, July 17, 2007 7:04 PM
*Subject:* Building A Custom Logger
Has anyone had experience building a custom logger?
Here is what I am doing. I have created my own interface describing what
the kinds of things a logger needs to be able to log in my environment
(let's call it IInterface) like security audits, performance statistics,
etc. This interface extends ILog as well.
Thus: IInterface : ILog
Then, I have an implementation of IInterface and we'll call that
InterfaceImpl which also inherits from LogImpl (in order to satisfy the ILog
interface).
Like so: InterfaceImpl : LogImpl, IInterface
Now, when I go to put a logger in a class in which I want to perform
ILog _logger = LogManager.GetLogger(typeof(MyClass));
IInterface _logger = LogManager.GetLogger(typeof(MyClass));
...but this does not work (nor does anything else I have tried) because
GetLogger returns LogImpl which knows nothing of my IInterface.
Can anyone recommend anything that I can try without needing to modify the
LogManager (a sealed class)?
--
Kevin Parkinson
Musician / Software Developer
Edmonton Alberta Canada
Continue reading on narkive:
Loading...