Geeks With Blogs
Tim Huffam Dotting the I and crossing the T of I.T.

This is something I wanted to do as a quick work around (rather than writing my own log4net appender) to being able to write SuccessAudit and FailureAudit events to the event log (something which log4net does not support out the box).

Here's the method I used:

private static ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static log4net.Appender.EventLogAppender _eventLogAppender = null;

// This only works if you have an EventLogAppender configured.
public static void WriteToEventLog(string Message, System.Diagnostics.EventLogEntryType Type)
{
    if (_eventLogAppender == null)
    {
        log4net.Appender.IAppender[] appenders = _Log.Logger.Repository.GetAppenders();
        foreach (log4net.Appender.IAppender appender in appenders)
        {
            if (appender is log4net.Appender.EventLogAppender)
            {
               _eventLogAppender = (log4net.Appender.EventLogAppender)appender;
               break;

            }
        }
    }

    if (_eventLogAppender != null) // Confirm an EventLogAppender has been configured
        System.Diagnostics.EventLog.WriteEntry(_eventLogAppender.ApplicationName, Message, Type);

}

Tim

 

Posted on Thursday, December 7, 2006 2:47 PM C# .NET , ASP.NET , Log4Net , Half Baked | Back to top


Comments on this post: Manually writing to the same windows event log as log4net

# re: Manually writing to the same windows event log as log4net
Requesting Gravatar...
This is the right information I am looking for. Thanks so much!
Left by Vietnam Airfares on Dec 20, 2011 11:55 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net