Log Exception with Event Log : Log « Development Class « C# / C Sharp






Log Exception with Event Log

      
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Open.MOF.Messaging
{
    public class EventLogUtility
    {
        private const string _constEventLogSource = "Message Oriented Framework";
        private const int _constEventLogId = 1001;

        public static void LogException(System.Exception ex)
        {
            LogErrorMessage(FormatExceptionMessage(ex));
        }

        public static void LogInformationMessage(string message)
        {
            System.Diagnostics.EventLog.WriteEntry(_constEventLogSource, message, EventLogEntryType.Information, _constEventLogId);
        }

        public static void LogWarningMessage(string message)
        {
            System.Diagnostics.EventLog.WriteEntry(_constEventLogSource, message, EventLogEntryType.Warning, _constEventLogId);
        }

        public static void LogErrorMessage(string message)
        {
            System.Diagnostics.EventLog.WriteEntry(_constEventLogSource, message, EventLogEntryType.Error, _constEventLogId);
        }

        private static string FormatExceptionMessage(System.Exception ex)
        {
            StringBuilder sbExceptionMessage = new StringBuilder();

            sbExceptionMessage.Append("An Exception has been raised:\r\n\r\n");
            sbExceptionMessage.Append("");
            sbExceptionMessage.Append(ex.Message);
            sbExceptionMessage.Append("\r\n");
            sbExceptionMessage.Append(ex.Source);
            sbExceptionMessage.Append("\r\n");
            sbExceptionMessage.Append(ex.StackTrace);

            if (ex.InnerException != null)
            {
                sbExceptionMessage.Append("\r\n\r\nAdditional Exception details:\r\n\r\n");
                sbExceptionMessage.Append(ex.InnerException.Message);
                sbExceptionMessage.Append("\r\n");
                sbExceptionMessage.Append(ex.InnerException.Source);
                sbExceptionMessage.Append("\r\n");
                sbExceptionMessage.Append(ex.InnerException.StackTrace);

                if (ex.InnerException.InnerException != null)
                {
                    sbExceptionMessage.Append("\r\n\r\nAdditional Exception details:\r\n\r\n");
                    sbExceptionMessage.Append(ex.InnerException.InnerException.Message);
                    sbExceptionMessage.Append("\r\n");
                    sbExceptionMessage.Append(ex.InnerException.InnerException.Source);
                    sbExceptionMessage.Append("\r\n");
                    sbExceptionMessage.Append(ex.InnerException.InnerException.StackTrace);
                }
            }

            return sbExceptionMessage.ToString();
        }


    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Demonstrates registering and event source and writing to the Application LogDemonstrates registering and event source and writing to the Application Log
2.Add a EventLogTraceListener to the listener collection and writing error messages to the Application log
3.Test Log
4.Set Text Output To Event LogSet Text Output To Event Log
5.Log utility based on File with File.AppendAllText
6.Log message with StreamWriter
7.File Logger
8.Log error to a file
9.Logger
10.Log Utility
11.Logger class
12.Logger 2
13.Instantiates a Trace log for detailed tracking of an application's internal activities.
14.Your own Logger
15.Implements a TextWriter for writing information to the debugger log.