Cloud listener

The KissLogApiListener saves the logs to kisslog.net (or KissLog on-premises).

KissLogApiListener output

KissLogApiListener output

Usage

protected void Application_Start()
{
    string organizationId = ConfigurationManager.AppSettings["KissLog.OrganizationId"];
    string applicationId = ConfigurationManager.AppSettings["KissLog.ApplicationId"];

    ILogListener cloudListener = new KissLogApiListener(new Application(organizationId, applicationId))
    {
        ApiUrl = "https://api.kisslog.net"
    };

    KissLogConfiguration.Listeners.Add(cloudListener);
}

Get the “KissLog.OrganizationId” and the “KissLog.ApplicationId” values from the Application Api Keys page.

Trigger events

KissLogApiListener is saving the logs at the end of the HTTP request by using the OnFlush() event.

GET /Products/Details                   <---- OnBeginRequest()


ILogger logger = Logger.Factory.Get();

logger.Debug("Debug message");          <---- OnMessage()


HTTP 200 OK                             <---- KissLogApiListener is executed
Simplified implementation
public class KissLogApiListener : ILogListener
{
    public void OnBeginRequest(HttpRequest httpRequest, ILogger logger)
    {
        // do nothing
    }

    public void OnMessage(LogMessage message, ILogger logger)
    {
        // do nothing
    }

    public void OnFlush(FlushLogArgs args, ILogger logger)
    {
        IKissLogApi kissLogApi = new KissLogRestApi("https://api.kisslog.net");
        kissLogApi.SaveAsync(args).ConfigureAwait(false);
    }
}

Console applications

For Console applications:

  • the listener needs to be flushed manually (line 18)

  • UseAsync flag must be set to false (line 26)

 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
28
29
30
31
static void Main(string[] args)
{
    ConfigureKissLog();

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

    try
    {
        logger.Info("Executing main");
    }
    catch(Exception ex)
    {
        logger.Error(ex);
        throw;
    }
    finally
    {
        Logger.NotifyListeners(logger);
    }

    static void ConfigureKissLog()
    {
        ILogListener cloudListener = new KissLogApiListener(new Application("OrganizationId", "ApplicationId"))
        {
            ApiUrl = "https://api.kisslog.net",
            UseAsync = false
        };

        KissLogConfiguration.Listeners.Add(cloudListener);
    }
}