c# writing to the event viewer

后端 未结 3 1920
时光取名叫无心
时光取名叫无心 2020-12-23 15:57

I\'m trying to write to the event viewer in my c# code, but I\'m getting the wonderful \"Object reference not set to an instance of an object\" message. I\'d appreciate som

相关标签:
3条回答
  • 2020-12-23 16:37

    The problem is probably that you are trying to create an event source in a log that doesn't exist. You need to specify the "Application" log.

    Try changing it to:

    if (!EventLog.SourceExists(cs))
       EventLog.CreateEventSource(cs, "Application");    
    
    EventLog.WriteEntry(cs, message, EventLogEntryType.Error);
    

    Also: Inside of sharepoint, if the app is running as logged in user(via windows auth or delegation), the user won't have access to create the event source. If this is the case, one trick is to create the event using a ThreadPool thread, which when created, will have the security context of the user the App Pool is running as.

    0 讨论(0)
  • 2020-12-23 16:49
       private void WriteEventLogToFile()
        {
            try
            {
                using (EventLog eventLog = new EventLog("Application"))
                {
                 // source for your event 
                    eventLog.Source = "IAStorDataMgrSvc";
    
                 // Syntax details
                // eventLog.WriteEntry("details",type of event,event id);
                 eventLog.WriteEntry("Hard disk Failure details", EventLogEntryType.Information, 11);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    
    0 讨论(0)
  • 2020-12-23 17:02

    Here's how I implemented Event logging. I created a generic ILogger interface so I can swap in different logging mechanisms:

    interface ILogger
    {
        void Debug(string text);
    
        void Warn(string text);
    
        void Error(string text);
        void Error(string text, Exception ex);
    }
    

    My implementation class is very simple:

    class EventLogger : ILogger
    {
        public void Debug(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Information);
        }
    
        public void Warn(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Warning);
        }
    
        public void Error(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Error);
        }
    
        public void Error(string text, Exception ex)
        {
            Error(text);
            Error(ex.StackTrace);
        }
    }
    

    Note that I do not instantiate EventLog. To use my logger class I just have the following reference (you could have this returned by a static factory method):

    private static readonly ILogger log = new EventLogger();
    

    And the actual usage is like this:

    try
    {
        // business logic
    }
    catch (Exception ex)
    {
        log.Error("Exception in MyMethodName()", ex);
    }
    
    0 讨论(0)
提交回复
热议问题