JD653687

I am upgrading to Visual Studio 2005 and have a problem with the following, hope someone can help me out:

{

EventLog aLogs = new EventLog();

aLogs.Log = "System";

//aLog.MachineName = machine;

aLogs.EntryWritten += new EntryWrittenEventHandler(OnSysEntryWritten);

aLogs.EnableRaisingEvents = true;

}

private static void OnSysEntryWritten(Object source, EntryWrittenEventArgs e)

{

if (e.Entry.EntryType == EventLogEntryType.Error)

{

string mBericht;

mBericht = "Source: " + e.Entry.Source + "\r\n" + "Datum/Tijd: " + e.Entry.TimeGenerated + "\r\n" + "Category: " + e.Entry.Category + "\r\n" + "Message: " + e.Entry.Message + "\r\n" + "EntryType: " + e.Entry.EntryType + "\r\n" + "EventID: " + e.Entry.EventID + "\r\n" + "UserName: " + e.Entry.UserName;

Verstuur_email("Event Error " + e.Entry.EventID + " " + e.Entry.Source , mBericht, "", "");

}

}

The e.Entry.EventID is working but gives a warning, "This Property has been deprecated." Tried to work it out myself but i can't find how to.

It should be replaced with System.Diagnostics.EventLogEntry.EventID



Re: .NET Base Class Library Eventid missing in Framework 2.0

TaylorMichaelL

This was asked before. Here was the final answer.

"Ahh I see where you're getting at I believe. EventID is the actual value that you want but it is deprecated. However EventID is actually InstanceID with the top 2 bits masked off. Therefore if you take the InstanceID and mask off the top 2 bits you'll get the EventID as shown in EV.

entry.EventID == entry.InstanceID & 0x3FFFFFFF
"

Michael Taylor - 2/16/07
http://p3net.mvps.org





Re: .NET Base Class Library Eventid missing in Framework 2.0

JD653687

Thank you for your response.

I still get some strange values
Entry ID 6011
Entry ID 6009
Entry ID 6005
Entry ID 263
Entry ID 39452806
Entry ID 39452806
Entry ID 1008
Entry ID 20003
Entry ID 20001
Entry ID 1008

The 39452806 are warnings wirh Event ID 134





Re: .NET Base Class Library Eventid missing in Framework 2.0

TaylorMichaelL

I don't quite know how to explain the disparent values. One thing I will point out though is that you'll ultimately be getting back an int so you should truncate the InstanceID (which is a long) to an int before doing the binary operation. Maybe this will resolve your issue.

Michael Taylor - 2/17/07
http://p3net.mvps.org





Re: .NET Base Class Library Eventid missing in Framework 2.0

JD653687

Thanks,

This did it

int EventID = (short)(e.Entry.InstanceId) & 0x3FFFFFFF;





Re: .NET Base Class Library Eventid missing in Framework 2.0

Kev Sout

Doesn't that AND everything in the 16 bit short, and therefore unnecessary That is, it's the same as
int EventID = short(e.Entry.InstanceID & 0x3FFFFFFF);
and
int EventID = short(e.Entry.InstanceID & 0xFFFF);
and
int EventID = short(e.Entry.InstanceID);

And wasn't eventID a 16-bit value anyway So what's all this about masking two bits of a 32-bit value (InstanceID) to get the eventID

In many cases, e.Entry.InstanceID & 0x3FFFFFFF doesn't equal the value that Event Viewer displays, which I assume is the eventID - in 16 bits

I'm obviously confused. Could someone clear this up
Thanks,
Kev




Re: .NET Base Class Library Eventid missing in Framework 2.0

AndreyKa

This method does not work for me also. I need to get the exact value as in Event Viewer, from the EventLogEntry object.

If i use the method, mentioned above, I am getting the wrong values in my case.

Any additional way to do it





Re: .NET Base Class Library Eventid missing in Framework 2.0

TaylorMichaelL

The original poster was interested in getting the equivalent functionality of EventId and not necessarily lining it up with what Event Viewer showed. Therefore the posted code InstanceId & 0x3FFFFFFF would give them the same value as EventId from v1.1. However InstanceId is a long whereas the original value was an int (not a short) so truncation needs to occur. The OP said that casting to short worked but it should actually be truncated to an int to get the full value like so:

(int)(InstanceId) & 0x3FFFFFFF

This will give the same value as EventId in all cases. Whether that maps back to Event Viewer is a different story altogether but if one is wrong then they both will be.

Michael Taylor - 2/28/07
http://p3net.mvps.org





Re: .NET Base Class Library Eventid missing in Framework 2.0

AndreyKa

The problem is, that it simple does not work with me.

I am generating event to be created. Its displayed in Event Log viewer as event 61466.

Same time i am catching the event

EntryWritten,

and there i am trying to get the event id by using the method above =

int eventID = (int)(EventLog.EventID) & 0x3FFFFFFF;

and that does not give me te right result. I am getting totallydifferent numbers.





Re: .NET Base Class Library Eventid missing in Framework 2.0

TaylorMichaelL

I think there is some confusion here so I'll try and explain the details a little better.

Firstly the OP wanted to get equivalent functionality to EventId which was deprecated in v2. The formual: (int)(InstanceId) & 0x3FFFFFFF will give you that.

Secondly the event ID has little meaning and does not match the value shown in EV. It shows you the full ID as defined for the event record. So how do you get the same value from EV Pretty straightforward once you understand the layout.

EV does not show you the actual event ID. Instead it shows you only the code portion of the ID. The code is simply the last 2 bytes of the event ID. So if you want to get the same value as shown in EV then do this: (int)(InstanceId) & 0xFF.

Hope this clarifies things.

Michael Taylor - 2/28/07
http://p3net.mvps.org





Re: .NET Base Class Library Eventid missing in Framework 2.0

AndreyKa

Thank you for the help.

The (int)(InstanceId) & 0xFF didnt worked for me thou.

I used the (int)(InstanceId) & 0x3FFFF

and only then got the right results.





Re: .NET Base Class Library Eventid missing in Framework 2.0

Kev Sout

Thanks Michael, all clear now (even though you meant 0xFFFF) :-)