Local text files output

In the following example, we will create a log listener which saves the log messages to local text files.


Create ITextFormatter

First, we implement the ITextFormatter interface, where we define the text format of the log messages.

public class MyTextFormatter : ITextFormatter
    public string Format(WebRequestProperties webRequestProperties)
        DateTime startDateTime = webRequestProperties.StartDateTime;
        HttpStatusCode statusCode = webRequestProperties.Response.HttpStatusCode;
        string absoluteUri = webRequestProperties.Url.AbsoluteUri;

        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"[{startDateTime:dd-MM-yyyy HH:mm:ss}]");
        sb.AppendLine($"{statusCode} {absoluteUri}");

        return sb.ToString();

    public string Format(LogMessage logMessage)
        return $"[{logMessage.LogLevel}] {logMessage.Message}";

Register the listener

Finally, we register the built-in LocalTextFileListener listener.

All the log files will be saved under ~/Logs directory.

protected void Application_Start()
        new LocalTextFileListener(
            new MyTextFormatter(),
            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs")