C # write to event viewer

I am trying to write to the event viewer in my C # code, but I get the wonderful message "Link to an object not installed on an instance of the object". I would appreciate help in this code, or what's wrong with it, or even the best way to do it. Here is what I have to write to the event log:

private void WriteToEventLog(string message) { string cs = "QualityDocHandler"; EventLog elog = new EventLog(); if (!EventLog.SourceExists(cs)) { EventLog.CreateEventSource(cs, cs); } elog.Source = cs; elog.EnableRaisingEvents = true; elog.WriteEntry(message); } 

And here, where I try to call it:

 private readonly Random _rng = new Random(); private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private string RandomString(int size) { try { char[] buffer = new char[size]; for (int i = 0; i < size; i++) { buffer[i] = _chars[_rng.Next(_chars.Length)]; } return new string(buffer); } catch (Exception e) { WriteToEventLog(e.ToString()); return null; } } 
+54
c # event-log
Jul 15 '09 at 19:12
source share
3 answers

Probably the problem is that you are trying to create an event source in a log that does not 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 sharepoint, if the application works as a registered user (through auth or Windows delegation), the user will not have access to create the event source. If so, one trick is to create an event using the ThreadPool thread, which when created will have a security context for the user running in the App Pool.

+87
Jul 15 '09 at 19:29
source share

This is how I implemented event logging. I created a common ILogger interface, so I can swap different registration 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); } } 

Please note that I am not creating an EventLog. To use my logger class, I just have the following link (you could return this static factory method):

 private static readonly ILogger log = new EventLogger(); 

And the actual use looks like this:

 try { // business logic } catch (Exception ex) { log.Error("Exception in MyMethodName()", ex); } 
+22
Jul 15 '09 at 19:28
source share
  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; } } 
+1
Feb 06 '17 at 9:14
source share



All Articles