Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from dgarciarubio/app-settings
Browse files Browse the repository at this point in the history
App settings support by adding specific extension methods

solves #8
  • Loading branch information
tsimbalar authored May 29, 2018
2 parents bdd47fd + ef7cde5 commit 8640321
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 29 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ _This package is designed for full framework ASP.NET applications. For ASP.NET C
This package is used in conjunction with _SerilogWeb.Classic_ and adds ASP.NET MVC specific enrichers.

## Enrichers
The following enrichers are available in the `SerilogWeb.Classic.Mvc.Enrichers` namespace:
- **MvcActionNameEnricher** : adds a property `MvcAction` containing the name of the *Action* being executed in the *MVC Controller*
- **MvcControllerNameEnricher** : adds a property `MvcController` containing the name of the *Controller* in which a *MVC Action* has executed
- **MvcRouteDataEnricher** : adds a property `MvcRouteData` containing the dictionary of the *RouteData*
- **MvcRouteTemplateEnricher** : adds a property `MvcRouteTemplate` containing the *route template* selected by Mvc routing
The following enrichers are available as extension methods from the `LoggerConfiguration.Enrich` API:
- **WithMvcActionName** : adds a property `MvcAction` containing the name of the *Action* being executed in the *MVC Controller*
- **WithMvcControllerName** : adds a property `MvcController` containing the name of the *Controller* in which a *MVC Action* has executed
- **WithMvcRouteData** : adds a property `MvcRouteData` containing the dictionary of the *RouteData*
- **WithMvcRouteTemplate** : adds a property `MvcRouteTemplate` containing the *route template* selected by Mvc routing


Usage :

```csharp
var log = new LoggerConfiguration()
.WriteTo.Console()
.Enrich.With<MvcRouteTemplateEnricher>()
.Enrich.With<MvcActionNameEnricher>()
.Enrich.WithMvcRouteTemplate()
.Enrich.WithMvcActionName()
.CreateLogger();
```

Expand All @@ -31,7 +31,19 @@ To override the property name of the added property:
```csharp
var log = new LoggerConfiguration()
.WriteTo.Console()
.Enrich.With(new MvcRouteTemplateEnricher("RouteTemplate")
.Enrich.WithMvcRouteTemplate("RouteTemplate")
.CreateLogger();
```

Enrichers can also be defined in a configuration file by using [Serilog.Settings.AppSettings](https://github.com/serilog/serilog-settings-appsettings) as follows:

```xml
<appSettings>
<add key="serilog:using:SerilogWeb.Classic.Mvc" value="SerilogWeb.Classic.Mvc"/>
<add key="serilog:enrich:WithMvcActionName"/>
<add key="serilog:enrich:WithMvcControllerName"/>
<add key="serilog:enrich:WithMvcRouteData"/>
<add key="serilog:enrich:WithMvcRouteTemplate"/>
</appSettings>
```

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
/// </summary>
public class MvcActionNameEnricher : BaseMvcContextInfoEnricher
{
public const string MvcActionPropertyName = "MvcAction";

public MvcActionNameEnricher()
: this("MvcAction")
: this(MvcActionPropertyName)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
/// </summary>
public class MvcControllerNameEnricher : BaseMvcContextInfoEnricher
{
public const string MvcControllerPropertyName = "MvcController";

public MvcControllerNameEnricher()
: this("MvcController")
: this(MvcControllerPropertyName)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
/// </summary>
public class MvcRouteDataEnricher : BaseMvcContextInfoEnricher
{
public const string MvcRouteDataPropertyName = "MvcRouteData";

public MvcRouteDataEnricher()
: this("MvcRouteData")
: this(MvcRouteDataPropertyName)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
/// </summary>
public class MvcRouteTemplateEnricher : BaseMvcContextInfoEnricher
{
public const string MvcRouteTemplatePropertyName = "MvcRouteTemplate";

public MvcRouteTemplateEnricher()
: this("MvcRouteTemplate")
: this(MvcRouteTemplatePropertyName)
{
}

Expand Down
13 changes: 9 additions & 4 deletions src/SerilogWeb.Classic.Mvc/SerilogWeb.Classic.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\..\packages\Serilog.2.5.0\lib\net45\Serilog.dll</HintPath>
<HintPath>..\..\packages\Serilog.2.6.0\lib\net45\Serilog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SerilogWeb.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SerilogWeb.Classic.3.0.20\lib\net45\SerilogWeb.Classic.dll</HintPath>
<Reference Include="SerilogWeb.Classic, Version=4.2.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SerilogWeb.Classic.4.2.41\lib\net45\SerilogWeb.Classic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -88,9 +90,12 @@
<Compile Include="Classic\Mvc\StoreMvcInfoInHttpContextActionFilter.cs" />
<Compile Include="Classic\Mvc\MvcRequestInfoKey.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerilogWebClassicMvcLoggerConfigurationExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="SerilogWeb.Classic.Mvc.nuspec" />
<None Include="SerilogWeb.snk" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Serilog.Configuration;
using SerilogWeb.Classic.Mvc.Enrichers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serilog
{
/// <summary>
/// Extends <see cref="LoggerConfiguration"/> to add enrichers for SerilogWeb.Classic.Mvc logging module
/// </summary>
public static class SerilogWebClassicMvcLoggerConfigurationExtensions
{
/// <summary>
/// Enrich log events with the name of the current MVC action.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <param name="propertyName">Name of the property to log.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithMvcActionName(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
string propertyName = MvcActionNameEnricher.MvcActionPropertyName)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new MvcActionNameEnricher(propertyName));
}

/// <summary>
/// Enrich log events with the controller name for the current MVC action.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <param name="propertyName">Name of the property to log.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithMvcControllerName(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
string propertyName = MvcControllerNameEnricher.MvcControllerPropertyName)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new MvcControllerNameEnricher(propertyName));
}

/// <summary>
/// Enrich log events with the route data for the current MVC action.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <param name="propertyName">Name of the property to log.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithMvcRouteData(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
string propertyName = MvcRouteDataEnricher.MvcRouteDataPropertyName)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new MvcRouteDataEnricher(propertyName));
}

/// <summary>
/// Enrich log events with the template for the current MVC action.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <param name="propertyName">Name of the property to log.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithMvcRouteTemplate(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
string propertyName = MvcRouteTemplateEnricher.MvcRouteTemplatePropertyName)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new MvcRouteTemplateEnricher(propertyName));
}
}
}
4 changes: 2 additions & 2 deletions src/SerilogWeb.Classic.Mvc/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Serilog" version="2.5.0" targetFramework="net45" />
<package id="SerilogWeb.Classic" version="3.0.20" targetFramework="net45" />
<package id="Serilog" version="2.6.0" targetFramework="net45" />
<package id="SerilogWeb.Classic" version="4.2.41" targetFramework="net45" />
</packages>
6 changes: 1 addition & 5 deletions test/SerilogWeb.Classic.Mvc.Test/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public class MvcApplication : System.Web.HttpApplication
protected void Application_Start()
{
Serilog.Log.Logger = new LoggerConfiguration()
.Enrich.With<MvcControllerNameEnricher>()
.Enrich.With<MvcRouteDataEnricher>()
.Enrich.With<MvcRouteTemplateEnricher>()
.Enrich.With<MvcActionNameEnricher>()
.WriteTo.Trace(new JsonFormatter())
.ReadFrom.AppSettings()
.CreateLogger();

AreaRegistration.RegisterAllAreas();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@
<HintPath>..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\..\packages\Serilog.2.5.0\lib\net45\Serilog.dll</HintPath>
<HintPath>..\..\packages\Serilog.2.6.0\lib\net45\Serilog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Settings.AppSettings, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\..\packages\Serilog.Settings.AppSettings.2.1.2\lib\net45\Serilog.Settings.AppSettings.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Sinks.Trace, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\..\packages\Serilog.Sinks.Trace.2.1.0\lib\net45\Serilog.Sinks.Trace.dll</HintPath>
</Reference>
<Reference Include="SerilogWeb.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SerilogWeb.Classic.3.0.20\lib\net45\SerilogWeb.Classic.dll</HintPath>
<Reference Include="SerilogWeb.Classic, Version=4.2.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SerilogWeb.Classic.4.2.41\lib\net45\SerilogWeb.Classic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -175,7 +181,9 @@
<Content Include="fonts\glyphicons-halflings-regular.woff" />
<Content Include="fonts\glyphicons-halflings-regular.ttf" />
<Content Include="fonts\glyphicons-halflings-regular.eot" />
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<Content Include="Scripts\jquery-1.10.2.min.map" />
</ItemGroup>
<PropertyGroup>
Expand Down
11 changes: 11 additions & 0 deletions test/SerilogWeb.Classic.Mvc.Test/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

<add key="serilog:minimum-level" value="Debug" />
<add key="serilog:using:Trace" value="Serilog.Sinks.Trace" />
<add key="serilog:write-to:Trace"/>
<add key="serilog:write-to:Trace.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception} {Properties:j}"/>

<add key="serilog:using:SerilogWeb.Classic.Mvc" value="SerilogWeb.Classic.Mvc"/>
<add key="serilog:enrich:WithMvcActionName.propertyName" value="MvcActionNameTest"/>
<add key="serilog:enrich:WithMvcControllerName.propertyName" value="MvcControllerNameTest"/>
<add key="serilog:enrich:WithMvcRouteData.propertyName" value="MvcRouteDataTest"/>
<add key="serilog:enrich:WithMvcRouteTemplate.propertyName" value="MvcRouteTemplateTest"/>
</appSettings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
Expand Down
5 changes: 3 additions & 2 deletions test/SerilogWeb.Classic.Mvc.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<package id="Modernizr" version="2.6.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
<package id="Respond" version="1.2.0" targetFramework="net45" />
<package id="Serilog" version="2.5.0" targetFramework="net45" />
<package id="Serilog" version="2.6.0" targetFramework="net45" />
<package id="Serilog.Settings.AppSettings" version="2.1.2" targetFramework="net45" />
<package id="Serilog.Sinks.Trace" version="2.1.0" targetFramework="net45" />
<package id="SerilogWeb.Classic" version="3.0.20" targetFramework="net45" />
<package id="SerilogWeb.Classic" version="4.2.41" targetFramework="net45" />
<package id="WebGrease" version="1.5.2" targetFramework="net45" />
</packages>

0 comments on commit 8640321

Please sign in to comment.