Skip to content

ASP.NET WebAPI support for SerilogWeb.Classic

License

Notifications You must be signed in to change notification settings

serilog-web/classic-webapi

Repository files navigation

SerilogWeb.Classic.WebApi Build status

ASP.NET WebAPI support for SerilogWeb.Classic.

Package - SerilogWeb.Classic.WebApi | Platforms - .NET 4.5

This package is designed for full framework ASP.NET applications. For ASP.NET Core, have a look at Serilog.AspNetCore

This package is used in conjunction with SerilogWeb.Classic and adds :

  • support for logging WebAPI exceptions with Serilog
  • Web API specific enrichers

Enrichers

The following enrichers are available as extension methods from the LoggerConfiguration.Enrich API:

  • WithWebApiActionName : adds a property WebApiAction containing the name of the Action being executed in the Web API Controller
  • WithWebApiControllerName : adds a property WebApiController containing the name of the Controller in which a Web API Action has executed
  • WithWebApiRouteData : adds a property WebApiRouteData containing the dictionary of the RouteData
  • WithWebApiRouteTemplate : adds a property WebApiRouteTemplate containing the route template selected by Web API routing

Usage :

var log = new LoggerConfiguration()
    .WriteTo.Console()
    .Enrich.WithWebApiRouteTemplate()
    .Enrich.WithWebApiActionName()
    .CreateLogger();

To override the property name of the added property:

var log = new LoggerConfiguration()
    .WriteTo.Console()
    .Enrich.WithWebApiRouteTemplate("RouteTemplate")
    .CreateLogger();

Enrichers can also be defined in a configuration file by using Serilog.Settings.AppSettings as follows:

<appSettings>
    <add key="serilog:using:SerilogWeb.Classic.WebApi" value="SerilogWeb.Classic.WebApi"/>
    <add key="serilog:enrich:WithWebApiActionName" />
    <add key="serilog:enrich:WithWebApiControllerName" />
    <add key="serilog:enrich:WithWebApiRouteData" />
    <add key="serilog:enrich:WithWebApiRouteTemplate" />
</appSettings>