Log Event in Windows Operating System Using C#

Author posted by Jitendra on Posted on under category Categories c# and tagged as Tags with Leave a comment on Log Event in Windows Operating System Using C#

Tutorial on Logging Message in EventLog of Microsoft Windows.

Below is the Utility function used to log the message in Microsoft Windows Operating Systems.

class Program
    {
        static void Main(string[] args)
        {
            LogMessage("ShivaSoftBlog - Error Message", EventLogEntryType.Error);
            LogMessage("ShivaSoftBlog - Failure Audit Message", EventLogEntryType.FailureAudit);
            LogMessage("ShivaSoftBlog - Information Message", EventLogEntryType.Information);
            LogMessage("ShivaSoftBlog - Success Audit Message", EventLogEntryType.SuccessAudit);
            LogMessage("ShivaSoftBlog - Warning Message", EventLogEntryType.Warning);
            Console.WriteLine("LOg Success");
            Console.Read();
        }

        private static void LogMessage(string message, EventLogEntryType logType)
        {
            if (!EventLog.SourceExists("ShivaSoftBlog"))
                EventLog.CreateEventSource("ShivaSoftBlog", "Application");

            EventLog ncsLog = new EventLog();
            ncsLog.Source = "ShivaSoftBlog";
            ncsLog.WriteEntry(message, logType);

        }
    }

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *