EventLog Intermittent Exception

I have an intermittent problem with some code that writes to the Windows event log using the C # and .Net EventLog classes.

Basically, this code works fine every day, but very rarely do we start to get errors like this:

"System.ArgumentException: only the first eight characters of the user log name are significant, and there is already another log in the system using the first eight characters the name is specified. Name given:" Application ", name of the existing log:" Application ".

I can determine from other information in our logs that the affected call column looks like this: you can clearly see what I'm actually trying to write to the existing LB_Email ( LogEmail is called LogEmail ):

 public static void LogEmail(string to, string type) { string message = String.Format("{0}\t{1}\t{2}", DateTime.Now, to, type); Log(message, "LB_Email", EventLogEntryType.Information); } private static void Log(string message, string logName, EventLogEntryType type) { using (EventLog aLog = new EventLog()) { aLog.Source = logName; aLog.WriteEntry(message, type); } } 

As soon as the errors start, it seems that access to our LB_Email event LB_Email blocked in some way - viewing the properties in a particular event log shows most of the information greyed-out and unchangeable, and other processes do not seem to be able to enter this log too . However, I see an error (which uses the same Log method above) with a try-catch that is logged in the "LB_Error" log and continues to function properly.

I am calling this code from a multi-threaded application, but I have not been able to determine whether this code is thread safe or not.

I can also confirm that the journal issue is working fine again after killing and restarting the process ... and it had the appropriate settings to reuse the entries when it was full ... although I don't think that was the problem.

I would like to hear your thoughts and suggestions.

+3
source share
2 answers

The documentation states that:

You can only use the source to write to one log at a time

Therefore, I suspect that this problem is caused by a multi-threaded application calling the Log method more than once at a given time and for the same source.

I suggest that instead of a static class (or methods), a streaming singleton class be used to record these events.

EDIT:

Jon Skeet has a great singleplayer article .

If you don't want to implement a singleton class, you can do something like this:

  static readonly object lockObj = new object(); public static void LogEmail(string to, string type) { string message = String.Format("{0}\t{1}\t{2}", DateTime.Now, to, type); Log(message, "LB_Email", EventLogEntryType.Information); } private static void Log(string message, string logName, EventLogEntryType type) { lock (lockObj) { using (EventLog aLog = new EventLog()) { aLog.Source = logName; aLog.WriteEntry(message, type); } } } 

Hope this solves your problem.

+4
source

Thanks Bruno

So, I am mistaken in thinking that the EventLog instance in the log method is different from the EventLog instance in the same method call in another thread? Or am I just confused about instances of objects inside a static method?

OK, so I have several wrapper methods for the Log (...) method. If I moved the log method to a singleton class, changed the wrappers (LogEmail, LogXxxx, LogYyy, etc., then I could support my Log.Zzzz interfaces the same way, but use the security of the singleton LogSingleton.Instance.Log (...) from current logs or because I want to write to different magazines, will each of them have its own LogSingletonXxx?

You can say that I am confused :) Yes - I would really appreciate the synchronization code :)

Nij

0
source

All Articles