Text file listener

The LocalTextFileListener saves the logs on local text files.

LocalTextFileListener output

LocalTextFileListener output

Usage

protected void Application_Start()
{
    ILogListener textListener = new LocalTextFileListener(@"C:\\my-application\\logs")
    {
        FlushTrigger = FlushTrigger.OnMessage
    };

    KissLogConfiguration.Listeners.Add(textListener);
}

Trigger events

  • FlushTrigger.OnMessage

The listener will save the logs as soon as they are created, using OnMessage() event.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Program
{
    static void Main(string[] args)
    {
        ConfigureKissLog();

        ILogger logger = new Logger(url: "Main");   <---- LocalTextFileListener is executed

        logger.Info("Preparing to execute Main");   <---- LocalTextFileListener is executed

        logger.Warn("Entering infinite loop");      <---- LocalTextFileListener is executed

        while (true) { }

        Logger.NotifyListeners(logger);
    }
}
  • FlushTrigger.OnFlush

The listener will save the logs when Logger.NotifyListeners() method is triggered, using OnFlush() event.

For web applications this happens automatically at the end of the HTTP request.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Program
{
    static void Main(string[] args)
    {
        ConfigureKissLog();

        ILogger logger = new Logger(url: "Main");

        logger.Info("Preparing to execute Main");

        logger.Warn("Entering infinite loop");

        Logger.NotifyListeners(logger);             <---- LocalTextFileListener is executed
    }
}

Console applications

For Console applications it is recommended to use the FlushTrigger.OnMessage.

This will ensure that the logs will be saved regardless of the method execution time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Program
{
    static void Main(string[] args)
    {
        ConfigureKissLog();

        ILogger logger = new Logger(url: "Main");

        logger.Info("Preparing to execute Main");

        logger.Warn("Entering infinite loop");

        while (true) { }

        // will never be triggered
        Logger.NotifyListeners(logger);
    }

    static void ConfigureKissLog()
    {
        KissLogConfiguration.Listeners.Add(new LocalTextFileListener(@"C:\\my-application\\logs")
        {
            FlushTrigger = FlushTrigger.OnMessage
        });
    }
}
FlushTrigger.OnMessage output

FlushTrigger.OnMessage output