Configuration

KissLog can be configured by using the KissLogConfiguration class.

public static class KissLogConfiguration
{
    public static List<ILogListener> Listeners { get; }
    public static Options Options { get; }
}

Listeners

Represents the collection of log listeners.

public Startup()
{
    KissLogConfiguration.Listeners.Add(new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(
        Configuration["KissLog.OrganizationId"],
        Configuration["KissLog.ApplicationId"])
    ));

    KissLogConfiguration.Listeners.Add(
        new LocalTextFileListener(
            new TextFileFormatter(),
            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs")
        )
    );
}

Options

Provides a number of properties and runtime handlers used to customize the logs output and behavior.

public class Options
{
    public JsonSerializerSettings JsonSerializerSettings { get; }

    public Options AppendExceptionDetails(Func<Exception, string> handler);
    public Options GetUser(Func<RequestProperties, UserDetails> handler);
    public Options ShouldLogRequestClaim(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogRequestCookie(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogRequestFormData(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogRequestHeader(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogRequestInputStream(Func<ILogListener, FlushLogArgs, bool> handler);
    public Options ShouldLogRequestQueryString(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogRequestServerVariable(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ShouldLogResponseBody(Func<ILogListener, FlushLogArgs, bool, bool> handler);
    public Options ShouldLogResponseHeader(Func<ILogListener, FlushLogArgs, string, bool> handler);
    public Options ToggleListener(Func<ILogListener, FlushLogArgs, bool> handler);
}

JsonSerializerSettings

Specifies the Json.NET settings used when serializing log messages.

public Startup()
{
    KissLogConfiguration.Options
        .JsonSerializerSettings.Converters.Add(new StringEnumConverter());
}

AppendExceptionDetails

Adds additional information about a captured exception.

public Startup()
{
    KissLogConfiguration.Options
        .AppendExceptionDetails((Exception ex) =>
        {
            // log EntityFramework validation errors
            if (ex is DbEntityValidationException dbException)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("DbEntityValidationException:");

                foreach (var error in dbException.EntityValidationErrors.SelectMany(p => p.ValidationErrors))
                {
                    sb.AppendLine($"Field: {error.PropertyName}, Error: {error.ErrorMessage}");
                }

                return sb.ToString();
            }

            return null;
        });
}

GetUser

Gets the logged-in user details, used for KissLog.net user interface.

public Startup()
{
    KissLogConfiguration.Options
        .GetUser((RequestProperties request) =>
        {
            string userName = request.Claims?.FirstOrDefault(p => string.Compare(p.Key, "UserName", true) == 0).Value;
            string emailAddress = request.Claims?.FirstOrDefault(p => string.Compare(p.Key, "EmailAddress", true) == 0).Value;

            return new UserDetails
            {
                Name = userName,
                EmailAddress = emailAddress
            };
        });
}

ShouldLogRequestClaim

Determines if the HTTP Request Claim should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestClaim((ILogListener listener, FlushLogArgs args, string claimName) => true);
}

ShouldLogRequestCookie

Determines if the HTTP Request Cookie should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestCookie((ILogListener listener, FlushLogArgs args, string cookieName) => false);
}

ShouldLogRequestFormData

Determines if the HTTP Request FormData parameter should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestFormData((ILogListener listener, FlushLogArgs args, string key) =>
        {
            // do not log "CreditCard" parameter
            if (string.Compare(key, "CreditCard", true) == 0)
                return false;

            return true;
        });
}

ShouldLogRequestHeader

Determines if the HTTP Request Header should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestHeader((ILogListener listener, FlushLogArgs args, string headerName) => true);
}

ShouldLogRequestInputStream

Determines if the HTTP Request InputStream should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestInputStream((ILogListener listener, FlushLogArgs args) => true);
}

ShouldLogRequestQueryString

Determines if the HTTP Request QueryString parameter should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestQueryString((ILogListener listener, FlushLogArgs args, string key) => true);
}

ShouldLogRequestServerVariable

Determines if the HTTP Request ServerVariable parameter should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogRequestServerVariable((ILogListener listener, FlushLogArgs args, string name) => true);
}

ShouldLogResponseBody

Determines if the HTTP response content should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogResponseBody((ILogListener listener, FlushLogArgs args, bool defaultValue) =>
        {
            if ((int) args.WebRequestProperties.Response.HttpStatusCode >= 400)
            {
                // explicitly log the ResponseBody if the HTTP request was unsuccessful
                return true;
            }

            // use the defaultValue (which is calculated based on the Response Content-Type header)
            return defaultValue;
        });
}

ShouldLogResponseHeader

Determines if the HTTP Response Header should be logged or not.

public Startup()
{
    KissLogConfiguration.Options
        .ShouldLogResponseHeader((ILogListener listener, FlushLogArgs args, string headerName) =>
        {
            // do not log "X-Token-Auth" header
            if (string.Compare(key, "X-Token-Auth", true) == 0)
                return false;

            return true;
        });
}

ToggleListener

Determines if a log listener should be used or not.

public Startup()
{
    KissLogConfiguration.Options
        .ToggleListener((ILogListener listener, FlushLogArgs args) =>
        {
            string url = args.WebRequestProperties.Url.LocalPath;
            int statusCode = (int)args.WebRequestProperties.Response.HttpStatusCode;

            if (statusCode == 200 && url.Contains("/api/status"))
            {
                if (listener.GetType() == typeof(LocalTextFileListener))
                {
                    return false;
                }
            }

            return true;
        });
}