Skip to content

WebApiContrib/WebApiContrib.Tracing.Slab

Repository files navigation

WebAPIContrib

Community Contributions for ASP.NET Web API

WebApiContrib.Tracing.Slab

Visit the project website for more information and documentation.

This package can be downloaded as a NuGet package: https://www.nuget.org/packages/WebApiContrib.Tracing.Slab/

Here's how you can activate tracing with slab in your Web API config

	config.EnableSystemDiagnosticsTracing();
    config.Services.Replace(typeof(ITraceWriter), new SlabTraceWriter());

Here's how you could use the Slab Action Filter for request, response logging

    // Here you can log a resquest/response  messages.
    [SlabLoggingFilterAttribute]
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }

Here's how you can log all unhandled exceptions to your slab log.

    // Do this if you want to log all unhandled exceptions
    config.Services.Add(typeof(IExceptionLogger), new SlabLoggingExceptionLogger());

Here's how you can configure your own EventSource for tracing.

	// Example using an example EventSource
    var exectueLogDict = new Dictionary<TraceLevel, Action<string>>();
    WebApiTracingCustom.RegisterLogger(exectueLogDict);
    config.EnableSystemDiagnosticsTracing();
    config.Services.Replace(typeof(ITraceWriter), new SlabTraceWriter(exectueLogDict));

and the EventSource

using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Web.Http.Tracing;

namespace WebApiContrib.Tracing.Slab.Demo
{
    [EventSource(Name = "WebApiTracing")]
    public class WebApiTracingCustom : EventSource
    {
        private const int TraceLevelFatal = 401;
        private const int TraceLevelError = 402;
        private const int TraceLevelInformational = 403;
        private const int TraceLevelLogAlways = 404;
        private const int TraceLevelVerbose = 405;
        private const int TraceLevelWarning = 406;

        public static void RegisterLogger(Dictionary<TraceLevel, Action<string>> exectueLogDict)
        {
            exectueLogDict.Add(TraceLevel.Info, Log.Informational);
            exectueLogDict.Add(TraceLevel.Debug, Log.Verbose);
            exectueLogDict.Add(TraceLevel.Error, Log.Error);
            exectueLogDict.Add(TraceLevel.Fatal, Log.Critical);
            exectueLogDict.Add(TraceLevel.Warn, Log.Warning);
        }

        public static readonly WebApiTracingCustom Log = new WebApiTracingCustom();

        [Event(TraceLevelFatal, Message = "TraceLevel.Fatal{0}", Level = EventLevel.Critical)]
        public void Critical(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelFatal, message);
            }          
        }

        [Event(TraceLevelError, Message = "TraceLevel.Error{0}", Level = EventLevel.Error)]
        public void Error(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelError, message);
            }
        }

        [Event(TraceLevelInformational, Message = "TraceLevel.Info{0}", Level = EventLevel.Informational)]
        public void Informational(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelInformational, message);
            }
        }

        [Event(TraceLevelLogAlways, Message = "WebApiTracing LogAlways{0}", Level = EventLevel.LogAlways)]
        public void LogAlways(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelLogAlways, message);
            }
        }

        [Event(TraceLevelVerbose, Message = "TraceLevel.Debug{0}", Level = EventLevel.Verbose)]
        public void Verbose(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelVerbose, message);
            }
        }

        [Event(TraceLevelWarning, Message = "TraceLevel.Warn{0}", Level = EventLevel.Warning)]
        public void Warning(string message)
        {
            if (IsEnabled())
            {
                WriteEvent(TraceLevelWarning, message);
            }
        }
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published