How to create a log file in C#? Please let me know.?

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    I suggest using the event log instead of creating your own. It is fairly straightforward.

    using System;

    using System.Diagnostics;

    using System.Threading;

    class MySample{

    public static void Main(){

    // Create the source, if it does not already exist.

    if(!EventLog.SourceExists("MySource"))

    {

    //An event log source should not be created and immediately used.

    //There is a latency time to enable the source, it should be created

    //prior to executing the application that uses the source.

    //Execute this sample a second time to use the new source.

    EventLog.CreateEventSource("MySource", "MyNewLog");

    Console.WriteLine("CreatedEventSource");

    Console.WriteLine("Exiting, execute the application a second time to use the source.");

    // The source is created. Exit the application to allow it to be registered.

    return;

    }

    // Create an EventLog instance and assign its source.

    EventLog myLog = new EventLog();

    myLog.Source = "MySource";

    // Write an informational entry to the event log.

    myLog.WriteEntry("Writing to event log.");

    }

    }

  • 1 decade ago

    You can use one the the open source logging tools for C#.

    Have a look at this:

    "http://csharp-source.net/ open-source/logging"

    (I added a space to be removed between .net/ and open because of the Yahoo! Answers ellipsis bug).

    Regards

Still have questions? Get your answers by asking now.