Tuesday, March 5, 2013

C# | How to write entry to Windows Event

Here's a sample code on how to write to Windows Event Logs.
Keep an eye on variable maximumCharacterThatTheWindowEventLogsCanHandled where I used 30,000 but for it's actually 32,766 :)

     private static void WriteToWindowsEventLog(string logs)  
     {  
       const string source = "biboyatienza Sample Windows Services";  
       const string log = "Application";  
       const int maximumCharacterThatTheWindowEventLogsCanHandled = 30000;  
       if (!EventLog.SourceExists(source))  
         EventLog.CreateEventSource(source, log);  
       string message = (logs.Length >= maximumCharacterThatTheWindowEventLogsCanHandled  
                   ? HttpUtility.HtmlEncode(logs).Substring(0, maximumCharacterThatTheWindowEventLogsCanHandled)  
                   : HttpUtility.HtmlEncode(logs));  
       EventLog.WriteEntry(source, message);  
     }  

No comments:

Post a Comment