Question

In my app I use EventLog (because I am working with money, so I need logging all the events what can occurs). EventLog works nice but the problem is, that my messages are coming too quickly (in 1 second I can have something about 3-5 events).
After those messages I cannot sort them properly in the EventLog (I need sort them by time). The problem is: if in 1s occurs 5events, those 5events are sorted "randomly" in the EventLog, cause it is logging only throught YYYY/MM/DD hh/mm/ss, but I need add milliseconds. Is there way how to achieve that?

My basic code how I am logging events:

    if (!EventLog.SourceExists(EventLogName))
        EventLog.CreateEventSource(ex.Message, EventLogName);
    EventLog log = new EventLog();
    log.Source = EventLogName;
    log.WriteEntry(ex.Message, EventLogEntryType.Error);

the most precise attributes in WriteEntry are:
-string source
-string message
-EventLogEntryType type
-int eventID
-short category
-byte[] rawData

but there is nothing where will be the time stored. Is there way how to write in basic Windows EventLog with milliseconds precision?

Was it helpful?

Solution

Why can't you use 3rd party libraries? Have you discussed this with whomever proscribed the use of 'non-Microsoft' libraries, assuming that's the restriction. Having such a blanket restriction is daft.

You can't get any other timestamping simply, but you could try a few hacks. The obvious one is to take the millisecond precision part (which is not recorded in the log) and use that as the EventID- it's application-specific, so you can use it how you like. If you have only a few events per second, then sorting on the eventlog date/time should be ok, you can visually establish the order of the events.

If it's too many, then you can export the event log into Excel or something and sort by both columns.

Obviously, you can filter on the source as well, to reduce the chaff.

You may want to look at Windows high speed logging: ETW in C#/.NET using System.Diagnostics.Tracing.EventSource as well, it may help, but not if you're only going to log to the EventLog.

Essentially, if your requirement is millisecond precision, then you shouldn't use the Windows eventlog, because it doesn't offer that. As has been pointed out, there are many, many logging solutions that offer what you need.

OTHER TIPS

Given that it is the Event Log that is recording the timestamp, it doesn't look like you can get millisecond precision.

Wouldn't you be better off writing to a standard text based log using something like log4net? The logs written by log4net do have milli-second precision as well as lots of other things like the identity of the class writing the entry. Chances are it is going to be much faster too for if/when you need to scale up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top