c# writing to the event viewer

后端 未结 3 1924
时光取名叫无心
时光取名叫无心 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.

提交回复
热议问题