Joseph Michael Pesch
VP Programming

ASP.Net Diagnostics Trace Listners (Text File and Event Log Samples)

by 1. November 2010 15:25

public static class Logging

{

 

  public static void AddTextWriterTraceListener()

  {

    string TempPath = Path.GetTempPath();

    TempPath = ConfigurationManager.AppSettings["LogPath"];

    if (!TempPath.EndsWith("\\")) TempPath += "\\";

    AddTextWriterTraceListener("UNDEFINED", TempPath

      + "Global.Logging.TextWriterTraceListener."

      + DateTime.Now.ToString("yyyyMMdd-hhmm")

      + ".txt");

  }

  public static void AddTextWriterTraceListener(string ApplicationName)

  {

    string TempPath = Path.GetTempPath();

    TempPath = ConfigurationManager.AppSettings["LogPath"];

    if (!TempPath.EndsWith("\\")) TempPath += "\\";

    AddTextWriterTraceListener(ApplicationName, TempPath

      + "Global.Logging.TextWriterTraceListener."

      + ApplicationName + "."

      + DateTime.Now.ToString("yyyyMMdd-hhmm")

      + ".txt");

  }

  public static void AddTextWriterTraceListener(string ApplicationName, string TextLogFilePath)

  {

    TextWriterTraceListener txtlog = new TextWriterTraceListener(TextLogFilePath);

    System.Diagnostics.Debug.Listeners.Add(txtlog);

  }

  public static void AddEventLogTraceListener()

  {

    AddEventLogTraceListener("Global.Logging.EventLogTraceListener.UNDEFINED");

  }

  public static void AddEventLogTraceListener(string ApplicationName)

  {

    EventLogTraceListener applog = new EventLogTraceListener("Application");

    applog.Name = ApplicationName;

    System.Diagnostics.Debug.Listeners.Add(applog);

  }

}

Tags:

[None]

Comments are closed