EventLog Intermittent Exception

I have an intermittent problem with some code that writes to a Windows Event Log, using C# and .Net's EventLog class.

Basically, this code works day-to-day perfectly, but very occasionally, we start getting errors like this:

"System.ArgumentException: Only the first eight characters of a custom log name are significant, and there is already another log on the system using the first eight characters of the name given. Name given: 'Application', name of existing log: 'Application'."

I can identify from the other information on our logs that the call stack affected is like this - You can clearly see I am in fact trying to write to an existing LB_Email log (LogEmail is called first):

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);
    }
}

Once the errors start occurring, it seems like access to our LB_Email eventlog is locked somehow - viewing properties on the particular eventlog shows most information greyed-out and unchangeable, and other processes appear to be prevented from logging to that log too. However, I am seeing the error (which uses the same Log method above) via a try-catch that logs to an 'LB_Error' log, and that continues to function as expected.

I am calling this code from a multi-threaded application, but I have been unable to identify if the code above is thread-safe or not.

I can also confirm that the log in question is working again fine after killing and restarting the process... and it had appropriate settings to reuse entries when it got full... though I don't think that was the issue.

I'd love to hear your thoughts and suggestions.

Answers


The documentation states that:

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

So I suspect this problem is caused by your multithreaded app calling the Log method more that once at a given time and for the same Source.

I suggest that, instead of a static class (or methods), you use a thread-safe singleton class for logging these events.

EDIT:

Jon Skeet has an excellent article on singletons.

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.


Need Your Help

Javascript direct confirm() function replacement

javascript confirm

I would like to replace the Javascript confirm() function to allow custom buttons instead of Yes/Cancel. I tried searching but all the solutions are event driven such as jquery dialog(where the cod...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.