Logging To Event Viewer Or Log File Using C# .NET

In many of our .NET projects, we need to log exceptions/errors/information in either Windows event log or a custom text Log file for debugging/review/reporting purposes. Here is a generic class written for logging, you can use this class to any of your C# .NET projects and use WriteToLogFile OR WriteToEventLog methods wherever required in you code.

  1. To achieve this, add ‘Common’ folder or project in your visual studio solution.

    Add a class file named ‘Logging’ and use the following code. Make sure to add all the required reference assemblies.

    Code:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Diagnostics;  
    6. using System.IO;  
    7. using System.Security.Permissions;  
    8.   
    9. namespace Common {  
    10.     /*This Class is used for logging messages to either a custom EventViewer or in a plain text file located on web server.*/  
    11.   
    12.     public class Logging {#region "Variables"  
    13.   
    14.         private string sLogFormat;  
    15.         private string sErrorTime;  
    16.  
    17.         #endregion  
    18.  
    19.  
    20.         #region "Local methods"  
    21.   
    22.         /* Write to Txt Log File*/  
    23.         public void WriteToLogFile(string sErrMsg) {  
    24.             try {  
    25.                 //sLogFormat used to create log format :  
    26.                 // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message  
    27.                 sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";  
    28.   
    29.                 //this variable used to create log filename format "  
    30.                 //for example filename : ErrorLogYYYYMMDD  
    31.                 string sYear = DateTime.Now.Year.ToString();  
    32.                 string sMonth = DateTime.Now.Month.ToString();  
    33.                 string sDay = DateTime.Now.Day.ToString();  
    34.                 sErrorTime = sYear + sMonth + sDay;  
    35.   
    36.                 //writing to log file  
    37.                 string sPathName = "C:\\Logs\\ErrorLog" + sErrorTime;  
    38.                 StreamWriter sw = new StreamWriter(sPathName + ".txt"true);  
    39.                 sw.WriteLine(sLogFormat + sErrMsg);  
    40.                 sw.Flush();  
    41.                 sw.Close();  
    42.             } catch (Exception ex) {  
    43.                 WriteToEventLog("MySite""Logging.WriteToLogFile""Error: " + ex.ToString(), EventLogEntryType.Error);  
    44.             }  
    45.         }  
    Write to Event Log
    1. public void WriteToEventLog(string sLog, string sSource, string message, EventLogEntryType level) {  
    2.     //RegistryPermission regPermission = new RegistryPermission(PermissionState.Unrestricted);  
    3.     //regPermission.Assert();  
    4.   
    5.     if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);  
    6.   
    7.     EventLog.WriteEntry(sSource, message, level);  
    8. }  
    9.  
    10. #endregion  
    11. }  
    12. }  
  2. Call WriteToLogFile AND/OR WriteToEventLog with appropriate parameters in other functions as required.

  3. Build & deploy the solution. Check Windows’ event viewer or custom Log file to see if it’s working.

    Happy Coding!!!


Recommended Free Ebook
Similar Articles