Skip to content

KissLog for Web applications

Catalin Gavan edited this page Nov 16, 2021 · 6 revisions

For web applications, KissLog creates and shares the same logger instance throughout the entire http request (connection).

Logger must be resolved using Logger.Factory.Get() factory method.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var logger = Logger.Factory.Get();
        logger.Trace("Hey there!");

        return View();
    }
}

Logger will be flushed automatically at the end of the http request, hence there is no need to explicitly call Logger.NotifyListeners().

Http properties

KissLog captures all the available http properties and makes them available via logger.DataContainer.HttpProperties container.

public ActionResult Index()
{
    var logger = Logger.Factory.Get();

    // Http properties container
    var httpProperties = logger.DataContainer.HttpProperties;

    // "GET"
    string httpMethod = httpProperties.Request.HttpMethod;
    
    // "http://localhost:64295/Home/Index"
    string url = httpProperties.Request.Url.ToString();

    // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
    string userAgent = httpProperties.Request.UserAgent;

    return View();
}
Property Value
Request.StartDateTime 2021-11-16T10:51:19.3270440Z
Request.Url http://localhost:64295/Home/Index
Request.UserAgent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Request.SessionId bgrp3mvbbwwuxgdbzsbs5i1e
Request.Properties.FormData [{"Key":"Email","Value":"p.adams@example.com"},{"Key":"FirstName","Value":"Adams"}]
Response.StatusCode 200
Response.EndDateTime 2021-11-16T10:51:22.6053760Z
Response.Properties.Headers [{"Key":"Content-Type","Value":"application/json; charset=utf-8"},{"Key":"Content-Length","Value":"43922"}]