LocalTextFileListener

LocalTextFileListener is saving the logs on local text files.

Usage

protected void Application_Start()
{
    KissLogConfiguration.Listeners.Add(new LocalTextFileListener(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
    {
        FlushTrigger = FlushTrigger.OnFlush // OnFlush | OnMessage
    });
}
LocalTextFileListener output

LocalTextFileListener output

Trigger events

FlushTrigger.OnMessage

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

FlushTrigger.OnFlush

The listener will save the logs at the end of the http request, using the OnFlush() event.

Console applications

For Console applications, it is best to use the FlushTrigger.OnMessage, so that the logs are saved as soon as they are created.

In the example below, even if we are missing the call to Logger.NotifyListeners(), the logs are still saved on the local file.

 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(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
        {
            FlushTrigger = FlushTrigger.OnMessage
        });
    }
}
FlushTrigger.OnMessage output

FlushTrigger.OnMessage output

Using FlushTrigger.OnMessage is useful for long-running tasks, or when try-catch-finally is not practicable.

 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
27
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ConfigureKissLog();

        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        ILogger logger = new Logger();

        logger.Info("Creating database...");

        var dbContext = new ApplicationDbContext();
        bool created = dbContext.Database.EnsureCreated();

        logger.Info("Database created: " + created);
    }

    private void ConfigureKissLog()
    {
        KissLogConfiguration.Listeners.Add(new LocalTextFileListener(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
        {
            FlushTrigger = FlushTrigger.OnMessage
        });
    }
}