Logs Output

During an Http Request, ILogger stores the log messages in memory.

Only at the end of the request, the captured information will be persisted to the output locations.

This significantly improves the application throughput, avoiding frequent I/O, Network or Database operations.

Contents

ILogListener

Log messages are saved using ILogListener targets.

KissLog comes with two built-in listeners:

Developers can create custom output targets by implementing the ILogListener interface.

Registering log listeners

Log listeners are registered at Application startup code, using KissLogConfiguration.Listeners container.

protected void Application_Start()
{
    KissLogConfiguration.Listeners.Add(new KissLogApiListener());

    KissLogConfiguration.Listeners.Add(new LocalTextFileListener());
}

Toggle log listeners

Log listeners can be instructed to omit specific logs, based on several criterias:

  1. Response Status Code
  2. Log levels
  3. Custom rules

Response Status Code

By setting MinimumResponseHttpStatusCode, a Log Listener will activate only if the specified condition is met.

In the example bellow, the listener will execute only if the Response was unsuccessful.

KissLogConfiguration.Listeners.Add(
    new LocalTextFileListener(new MyTextFormatter(), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
    {
        // capture only Status Code >= 400
        MinimumResponseHttpStatusCode = 400
    }
);

Log levels

By setting MinimumLogMessageLevel, a Log Listener will persist only the messages of which Log Level criteria is met.

In the example bellow, the listener will discard the log messages where Log Level < Warning, respectively Trace, Debug and Information.

Note: Even if all the log messages are discarded, the listener will still get activated.

KissLogConfiguration.Listeners.Add(
    new LocalTextFileListener(new MyTextFormatter(), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
    {
        // keep Warning, Error and Critical messages
        MinimumLogMessageLevel = LogLevel.Warning
    }
);

Custom rules

Developers can define custom rules for a log listener, by extending LogListenerParser class.

A common scenario is when an API is executed successively, therefore logging all the requests is unnecessary.

In the example bellow, the listener will ignore successful "/api/healthCheck" requests.

Creating MyLogListenerParser.cs

public class MyLogListenerParser : LogListenerParser
{
    public override bool ShouldLog(FlushLogArgs args, ILogListener logListener)
    {
        WebRequestProperties webProperties = args.WebRequestProperties;
        int responseStatusCode = (int)webProperties.Response.HttpStatusCode;
        string localPath = webProperties.Url.LocalPath;

        if(string.Compare(localPath, "/api/healthCheck", true) == 0 && responseStatusCode < 400)
        {
            // we don't log successful /api/healthCheck requests
            return false;
        }

        return base.ShouldLog(args, logListener);
    }
}

Providing the custom MyLogListenerParser to a log listener.

KissLogConfiguration.Listeners.Add(
    new LocalTextFileListener(new MyTextFormatter(), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
    {
        Parser = new MyLogListenerParser()
    }
);