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 Billy Barnum <b....@comcast.net> on 2005/09/23 21:55:03 UTC

How to log EventID with EventLogAppender?

I'm logging with EventLogAppender, and setting the EventSource field in the
Application Log using ApplicationName property. 

But I would like to log my own EventIds as well. Is there a way to do this
with log4net? I'm not talking about putting something in the message text,
but setting the actual eventID field in the log. I can't see a way. What am
I missing?


WILLIAM BARNUM
b.barnum@comcast.net
 



RE: How to log EventID with EventLogAppender?

Posted by Ron Grabowski <ro...@yahoo.com>.
Looks like there's already an issue open for this:

 http://issues.apache.org/jira/browse/LOG4NET-38

--- Ron Grabowski <ro...@yahoo.com> wrote:

> --- Billy Barnum <b....@comcast.net> wrote:
> 
> > Right. Log4Net error msgs all have the same eventId. This is
> exactly
> > what I
> > want to change.
> 
> The documentation for the EventLogAppender says:
> 
> "
> If the event has an EventID property set then this integer will be
> used
> as the event log event id.
> "
> 
> which means if you set an EventID property:
> 
>  log4net.ThreadContext.Properties["EventID"] = 5;
> 
> That value will be sent to the Event Log. I may have used incorrect
> casing on EventID in one of my earlier posts.
> 
> > I'm certainly no EventLog guru, but what I know is this: EventId is
> > not a PK
> > at all. It is, however, meant to be unique within an EventSource
> and
> > optionally an EventCategory; usage is up to the application. The
> idea
> > is
> > EventSource and EventCategory provide context, while EventId
> > identfies the
> > particular problem. 2 side notes:
> 
> The EventSource property for an event log entry is set via the
> EventLogAppender's ApplicatioName property. The default value is:
> 
>  System.Threading.Thread.GetDomain().FriendlyName
> 
> I don't think this is a value that needs to be settable on a per
> message basis.
> 
> > 1. Log4Net already provides built-in context with the logger
> property
> > that
> > you can put in your logging message. IMHO it would be a great
> log4net
> > enhancement if this was passed through to the EventCategory field
> in
> > the
> > EventLog for EventLogAppender in addition to or instead of putting
> it
> > in the
> 
> There's currently no way to pass in or set an Event Category value
> for
> an event log entry. EventLogAppender uses this overload when logging
> which doesn't pass in a value for event category:
> 
>  EventLog.WriteEntry(string, string, EventLogEntryType, int)
> 
> I agree that it would be nice if it were possible to specify an Event
> Category for each message logged or a default value for Event
> Category.
> Since we're already looking for EventID in the logging event's
> properties we may want to also look for EventCategory and use that
> value to call this overload:
> 
>  EventLog.WriteEntry(string, string, EventLogEntryType, int, short)
> 
> Do you think it would be useful to allow default values for these
> properties to be specified inside the config file and used if the
> EventID or EventCategory property keys aren't present?
> 
>  <eventId value="5" />
>  <eventCategory value="10" />
> 
> > msg. If I get some time here I'll dig in and do it myself. I
> haven't
> > actually built log4net yet; I've only been using the executable
> > having just
> > been using log4net for a week.
> 
> If you're going to use TortoiseCVS to checkout the files this post
> may
> be helpful:
> 
>
http://www.mail-archive.com/log4net-user@logging.apache.org/msg02060.html
> 
> Apache is going to be moving everything to SVN before the end of
> year.
> I thought the Logging Servics projects had a date set a few weeks ago
> to make their move. I don't think has happened yet.
> 
> > Back to issue at hand ... Ron, I tried your example code there,
> with
> > the
> > threadcontext/setting EventId property to 5, and all. Quick
> questions
> > if you
> > have time ... no hurry ...
> 
> I must not have read the documentation correctly...try EventID.
> 
> > 1. What's the diff between this threadcontext and MDC/NDC?
> 
> This is what the release notes have to say about it:
> 
> 
>
http://logging.apache.org/log4net/release/release-notes.html#1.2.9-new
> 
> MDC is now implemented using the ThreadContext.
> 
> > 2. I still don't see how that eventid 5 gets to the eventid field
> in
> > EventLogAppender without going the extension route (your 2nd
> > example). What
> > do you put in your xml file? How does that go anywhere other than
> the
> > message text?
> 
>  log4net.ThreadContext.Properties["EventID"] = 5;
>  log.Debug("Hello World");
> 
> I don't think writting your own ILog is as terrible an idea as people
> sometimes think it is. Log4net as we know it is really ILogger. The
> default implementation of ILogger is itself abstracted through the
> ILog
> interface. If you know your company or project is going to be logging
> a
> lot of additional information other than the just messages and
> exceptions, I would write a LogManager that returns a custom ILog:
> 
>  Company.Project.Logging.ILog
> 
> You could write your implementation in such a way that you can load
> up
> either log4net 1.2.0 beta 8, log4net 1.2.9 beta, NLog, or MS's
> Logging
> Block at runtime without having to reference those assemblies
> directly
> from your project.
> 
> > 3. When I used that threadcontext code, asp rebooted itself with
> err
> > msgs -
> > which is sometimes a sign of stack overflow. Was there some cleanup
> > and/or
> > stack popping I needed to do?
> 
> Posting the code you used and the error messages generated is always
> helpful :)
> 


RE: How to log EventID with EventLogAppender?

Posted by Ron Grabowski <ro...@yahoo.com>.
--- Billy Barnum <b....@comcast.net> wrote:

> Right. Log4Net error msgs all have the same eventId. This is exactly
> what I
> want to change.

The documentation for the EventLogAppender says:

"
If the event has an EventID property set then this integer will be used
as the event log event id.
"

which means if you set an EventID property:

 log4net.ThreadContext.Properties["EventID"] = 5;

That value will be sent to the Event Log. I may have used incorrect
casing on EventID in one of my earlier posts.

> I'm certainly no EventLog guru, but what I know is this: EventId is
> not a PK
> at all. It is, however, meant to be unique within an EventSource and
> optionally an EventCategory; usage is up to the application. The idea
> is
> EventSource and EventCategory provide context, while EventId
> identfies the
> particular problem. 2 side notes:

The EventSource property for an event log entry is set via the
EventLogAppender's ApplicatioName property. The default value is:

 System.Threading.Thread.GetDomain().FriendlyName

I don't think this is a value that needs to be settable on a per
message basis.

> 1. Log4Net already provides built-in context with the logger property
> that
> you can put in your logging message. IMHO it would be a great log4net
> enhancement if this was passed through to the EventCategory field in
> the
> EventLog for EventLogAppender in addition to or instead of putting it
> in the

There's currently no way to pass in or set an Event Category value for
an event log entry. EventLogAppender uses this overload when logging
which doesn't pass in a value for event category:

 EventLog.WriteEntry(string, string, EventLogEntryType, int)

I agree that it would be nice if it were possible to specify an Event
Category for each message logged or a default value for Event Category.
Since we're already looking for EventID in the logging event's
properties we may want to also look for EventCategory and use that
value to call this overload:

 EventLog.WriteEntry(string, string, EventLogEntryType, int, short)

Do you think it would be useful to allow default values for these
properties to be specified inside the config file and used if the
EventID or EventCategory property keys aren't present?

 <eventId value="5" />
 <eventCategory value="10" />

> msg. If I get some time here I'll dig in and do it myself. I haven't
> actually built log4net yet; I've only been using the executable
> having just
> been using log4net for a week.

If you're going to use TortoiseCVS to checkout the files this post may
be helpful:

http://www.mail-archive.com/log4net-user@logging.apache.org/msg02060.html

Apache is going to be moving everything to SVN before the end of year.
I thought the Logging Servics projects had a date set a few weeks ago
to make their move. I don't think has happened yet.

> Back to issue at hand ... Ron, I tried your example code there, with
> the
> threadcontext/setting EventId property to 5, and all. Quick questions
> if you
> have time ... no hurry ...

I must not have read the documentation correctly...try EventID.

> 1. What's the diff between this threadcontext and MDC/NDC?

This is what the release notes have to say about it:

 http://logging.apache.org/log4net/release/release-notes.html#1.2.9-new

MDC is now implemented using the ThreadContext.

> 2. I still don't see how that eventid 5 gets to the eventid field in
> EventLogAppender without going the extension route (your 2nd
> example). What
> do you put in your xml file? How does that go anywhere other than the
> message text?

 log4net.ThreadContext.Properties["EventID"] = 5;
 log.Debug("Hello World");

I don't think writting your own ILog is as terrible an idea as people
sometimes think it is. Log4net as we know it is really ILogger. The
default implementation of ILogger is itself abstracted through the ILog
interface. If you know your company or project is going to be logging a
lot of additional information other than the just messages and
exceptions, I would write a LogManager that returns a custom ILog:

 Company.Project.Logging.ILog

You could write your implementation in such a way that you can load up
either log4net 1.2.0 beta 8, log4net 1.2.9 beta, NLog, or MS's Logging
Block at runtime without having to reference those assemblies directly
from your project.

> 3. When I used that threadcontext code, asp rebooted itself with err
> msgs -
> which is sometimes a sign of stack overflow. Was there some cleanup
> and/or
> stack popping I needed to do?

Posting the code you used and the error messages generated is always
helpful :)

RE: How to log EventID with EventLogAppender?

Posted by Billy Barnum <b....@comcast.net>.
Right. Log4Net error msgs all have the same eventId. This is exactly what I
want to change.

I'm certainly no EventLog guru, but what I know is this: EventId is not a PK
at all. It is, however, meant to be unique within an EventSource and
optionally an EventCategory; usage is up to the application. The idea is
EventSource and EventCategory provide context, while EventId identfies the
particular problem. 2 side notes:

1. Log4Net already provides built-in context with the logger property that
you can put in your logging message. IMHO it would be a great log4net
enhancement if this was passed through to the EventCategory field in the
EventLog for EventLogAppender in addition to or instead of putting it in the
msg. If I get some time here I'll dig in and do it myself. I haven't
actually built log4net yet; I've only been using the executable having just
been using log4net for a week.

2. There is a very helpful site out there http://www.eventid.net/ that
allows searching by eventsource, eventcategory, eventid. You get to see what
others have posted as solutions when they've had nasty things show up in
their EventLog. Some of it is free, and there's premium, paid info as well.
It's saved my butt a few times and is a great addition to general debugging
toolboxes.

Back to issue at hand ... Ron, I tried your example code there, with the
threadcontext/setting EventId property to 5, and all. Quick questions if you
have time ... no hurry ...

1. What's the diff between this threadcontext and MDC/NDC?
2. I still don't see how that eventid 5 gets to the eventid field in
EventLogAppender without going the extension route (your 2nd example). What
do you put in your xml file? How does that go anywhere other than the
message text?
3. When I used that threadcontext code, asp rebooted itself with err msgs -
which is sometimes a sign of stack overflow. Was there some cleanup and/or
stack popping I needed to do?

-B

WILLIAM BARNUM
14076 Rosario Rd.
Anacortes, WA 98221-8559
Phone: 360.299.3830 Cell: 360.202.3670
b.barnum@comcast.net
 

-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
Sent: Friday, September 23, 2005 2:27 PM
To: Log4NET User
Subject: RE: How to log EventID with EventLogAppender?

I'm not very familiar with the Event Log so bear with me...

I looked at some entries in the Event Log on my computer and saw that
for similiar messages, the Event ID was always the same. I thought the
Event ID was similiar to a primary key in a database. In other words I
thought that I could somehow query the Event Loger for a particular
event based on its Event ID:

 // WRONG ???
 EventLogEntry eventLogEntry = eventLog.GetEvent(eventId);

but it looks like that's not the case.

Are you wanting to set a default Event ID for your application?

<appender name="EventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="5" />
</appender>

Or setup default Event IDs in such a way that each log4net Level has
its own default EventId?

<appender name="InfoEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="10" />
...
</appender>

<appender name="WarnEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="100" />
...
</appender>

<appender name="ErrorEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="1000" />
...
</appender>


RE: How to log EventID with EventLogAppender?

Posted by Ron Grabowski <ro...@yahoo.com>.
I'm not very familiar with the Event Log so bear with me...

I looked at some entries in the Event Log on my computer and saw that
for similiar messages, the Event ID was always the same. I thought the
Event ID was similiar to a primary key in a database. In other words I
thought that I could somehow query the Event Loger for a particular
event based on its Event ID:

 // WRONG ???
 EventLogEntry eventLogEntry = eventLog.GetEvent(eventId);

but it looks like that's not the case.

Are you wanting to set a default Event ID for your application?

<appender name="EventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="5" />
</appender>

Or setup default Event IDs in such a way that each log4net Level has
its own default EventId?

<appender name="InfoEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="10" />
...
</appender>

<appender name="WarnEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="100" />
...
</appender>

<appender name="ErrorEventLogAppender"
type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.SimpleLayout" />
<eventId value="1000" />
...
</appender>

--- Billy Barnum <b....@comcast.net> wrote:

> That doesn't look to bad, you're correct. But I'll have to read in
> the SDK
> about use of these ThreadContext properties; I'm still too new around
> here
> to see how such properties pass through to an EventLog.WriteEntry()
> or
> whatever. Thank you once again.
> 
> As long as you're asking: yes, I propose that eventID is such an
> integral
> part of writing to Windows event logs that it should be exposed more
> easily.
> I think a single integer parameter would be sufficient; after all the
> resource which EventLogAppender wraps only accepts integers.
> 
> WILLIAM BARNUM
> b.barnum@comcast.net
>  
> 
> -----Original Message-----
> From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
> Sent: Friday, September 23, 2005 1:43 PM
> To: Log4NET User
> Subject: RE: How to log EventID with EventLogAppender?
> 
> The extension folder name may be misleading. The implementation isn't
> very complex:
> 
> public void Info(int eventId, object message, System.Exception t)
> {
>  if (this.IsInfoEnabled)
>  {
>   LoggingEvent loggingEvent =  new LoggingEvent(...);
>   loggingEvent.Properties["EventID"] = eventId;
>   Logger.Log(loggingEvent);
>  }
> }
> 
> You could do basically the same thing using the normal ILog
> interface:
> 
> log4net.ThreadContext.Properties["EventId"] = 5;
> log.Info("Hello World");
> 	
> using(log4net.ThreadContext.Stacks["EventId"].Push("5"))
> {
>  log.Info("Hello World");
> }
> 
> How do you propose this functionality be made part of the core? Do
> you
> think the ILog interface members should be modified to accept a
> single
> integer parameter? What if someone's EventId is a float, GUID, or a
> string?
> 
>  
> 
> 


RE: How to log EventID with EventLogAppender?

Posted by Billy Barnum <b....@comcast.net>.
That doesn't look to bad, you're correct. But I'll have to read in the SDK
about use of these ThreadContext properties; I'm still too new around here
to see how such properties pass through to an EventLog.WriteEntry() or
whatever. Thank you once again.

As long as you're asking: yes, I propose that eventID is such an integral
part of writing to Windows event logs that it should be exposed more easily.
I think a single integer parameter would be sufficient; after all the
resource which EventLogAppender wraps only accepts integers.

WILLIAM BARNUM
b.barnum@comcast.net
 

-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
Sent: Friday, September 23, 2005 1:43 PM
To: Log4NET User
Subject: RE: How to log EventID with EventLogAppender?

The extension folder name may be misleading. The implementation isn't
very complex:

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

You could do basically the same thing using the normal ILog interface:

log4net.ThreadContext.Properties["EventId"] = 5;
log.Info("Hello World");
	
using(log4net.ThreadContext.Stacks["EventId"].Push("5"))
{
 log.Info("Hello World");
}

How do you propose this functionality be made part of the core? Do you
think the ILog interface members should be modified to accept a single
integer parameter? What if someone's EventId is a float, GUID, or a
string?

 


RE: How to log EventID with EventLogAppender?

Posted by Ron Grabowski <ro...@yahoo.com>.
The extension folder name may be misleading. The implementation isn't
very complex:

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

You could do basically the same thing using the normal ILog interface:

log4net.ThreadContext.Properties["EventId"] = 5;
log.Info("Hello World");
					using(log4net.ThreadContext.Stacks["EventId"].Push("5"))
{
 log.Info("Hello World");
}

How do you propose this functionality be made part of the core? Do you
think the ILog interface members should be modified to accept a single
integer parameter? What if someone's EventId is a float, GUID, or a
string?

--- Billy Barnum <b....@comcast.net> wrote:

> Ah, an extension. I was hoping to avoid that. You'd think that'd be
> part of
> the core. Nevermind, though. I'll check it out - and thank you very
> much.
> 
> 
> 
> WILLIAM BARNUM
> b.barnum@comcast.net
>  
> 
> -----Original Message-----
> From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
> Sent: Friday, September 23, 2005 1:04 PM
> To: Log4NET User
> Subject: Re: How to log EventID with EventLogAppender?
> 
> Do you want to do something like this:
> 
>  public interface IEventIDLog : ILog
>  {
>   void Info(int eventId, object message);
>   void Info(int eventId, object message, Exception t);
>   void Warn(int eventId, object message);
>   void Warn(int eventId, object message, Exception t);
>   void Error(int eventId, object message);
>   void Error(int eventId, object message, Exception t);
>   void Fatal(int eventId, object message);
>   void Fatal(int eventId, object message, Exception t);
>  }
> 
> If so, there is example code here:
> 
> 
> http://cvs.apache.org/viewcvs.cgi/logging-log4net/extensions/net/1.0/
> 
> That works with both 1.0 and 1.1 of the Framework.
> 
> --- Billy Barnum <b....@comcast.net> wrote:
> 
> > I'm logging with EventLogAppender, and setting the EventSource
> field
> > in the
> > Application Log using ApplicationName property. 
> > 
> > But I would like to log my own EventIds as well. Is there a way to
> do
> > this
> > with log4net? I'm not talking about putting something in the
> message
> > text,
> > but setting the actual eventID field in the log. I can't see a way.
> > What am
> > I missing?
> > 
> > 
> > WILLIAM BARNUM
> > b.barnum@comcast.net
> >  
> > 
> > 
> > 
> 
> 


RE: How to log EventID with EventLogAppender?

Posted by Billy Barnum <b....@comcast.net>.
Ah, an extension. I was hoping to avoid that. You'd think that'd be part of
the core. Nevermind, though. I'll check it out - and thank you very much.



WILLIAM BARNUM
b.barnum@comcast.net
 

-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
Sent: Friday, September 23, 2005 1:04 PM
To: Log4NET User
Subject: Re: How to log EventID with EventLogAppender?

Do you want to do something like this:

 public interface IEventIDLog : ILog
 {
  void Info(int eventId, object message);
  void Info(int eventId, object message, Exception t);
  void Warn(int eventId, object message);
  void Warn(int eventId, object message, Exception t);
  void Error(int eventId, object message);
  void Error(int eventId, object message, Exception t);
  void Fatal(int eventId, object message);
  void Fatal(int eventId, object message, Exception t);
 }

If so, there is example code here:

 http://cvs.apache.org/viewcvs.cgi/logging-log4net/extensions/net/1.0/

That works with both 1.0 and 1.1 of the Framework.

--- Billy Barnum <b....@comcast.net> wrote:

> I'm logging with EventLogAppender, and setting the EventSource field
> in the
> Application Log using ApplicationName property. 
> 
> But I would like to log my own EventIds as well. Is there a way to do
> this
> with log4net? I'm not talking about putting something in the message
> text,
> but setting the actual eventID field in the log. I can't see a way.
> What am
> I missing?
> 
> 
> WILLIAM BARNUM
> b.barnum@comcast.net
>  
> 
> 
> 


Re: How to log EventID with EventLogAppender?

Posted by Ron Grabowski <ro...@yahoo.com>.
Do you want to do something like this:

 public interface IEventIDLog : ILog
 {
  void Info(int eventId, object message);
  void Info(int eventId, object message, Exception t);
  void Warn(int eventId, object message);
  void Warn(int eventId, object message, Exception t);
  void Error(int eventId, object message);
  void Error(int eventId, object message, Exception t);
  void Fatal(int eventId, object message);
  void Fatal(int eventId, object message, Exception t);
 }

If so, there is example code here:

 http://cvs.apache.org/viewcvs.cgi/logging-log4net/extensions/net/1.0/

That works with both 1.0 and 1.1 of the Framework.

--- Billy Barnum <b....@comcast.net> wrote:

> I'm logging with EventLogAppender, and setting the EventSource field
> in the
> Application Log using ApplicationName property. 
> 
> But I would like to log my own EventIds as well. Is there a way to do
> this
> with log4net? I'm not talking about putting something in the message
> text,
> but setting the actual eventID field in the log. I can't see a way.
> What am
> I missing?
> 
> 
> WILLIAM BARNUM
> b.barnum@comcast.net
>  
> 
> 
>