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

Commit 8640321

Browse files
authored
Merge pull request #10 from dgarciarubio/app-settings
App settings support by adding specific extension methods solves #8
2 parents bdd47fd + ef7cde5 commit 8640321

12 files changed

+142
-29
lines changed

README.md

+20-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ _This package is designed for full framework ASP.NET applications. For ASP.NET C
99
This package is used in conjunction with _SerilogWeb.Classic_ and adds ASP.NET MVC specific enrichers.
1010

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

1818

1919
Usage :
2020

2121
```csharp
2222
var log = new LoggerConfiguration()
2323
.WriteTo.Console()
24-
.Enrich.With<MvcRouteTemplateEnricher>()
25-
.Enrich.With<MvcActionNameEnricher>()
24+
.Enrich.WithMvcRouteTemplate()
25+
.Enrich.WithMvcActionName()
2626
.CreateLogger();
2727
```
2828

@@ -31,7 +31,19 @@ To override the property name of the added property:
3131
```csharp
3232
var log = new LoggerConfiguration()
3333
.WriteTo.Console()
34-
.Enrich.With(new MvcRouteTemplateEnricher("RouteTemplate")
34+
.Enrich.WithMvcRouteTemplate("RouteTemplate")
3535
.CreateLogger();
3636
```
3737

38+
Enrichers can also be defined in a configuration file by using [Serilog.Settings.AppSettings](https://github.com/serilog/serilog-settings-appsettings) as follows:
39+
40+
```xml
41+
<appSettings>
42+
<add key="serilog:using:SerilogWeb.Classic.Mvc" value="SerilogWeb.Classic.Mvc"/>
43+
<add key="serilog:enrich:WithMvcActionName"/>
44+
<add key="serilog:enrich:WithMvcControllerName"/>
45+
<add key="serilog:enrich:WithMvcRouteData"/>
46+
<add key="serilog:enrich:WithMvcRouteTemplate"/>
47+
</appSettings>
48+
```
49+

src/SerilogWeb.Classic.Mvc/Classic/Mvc/Enrichers/MvcActionNameEnricher.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/// </summary>
66
public class MvcActionNameEnricher : BaseMvcContextInfoEnricher
77
{
8+
public const string MvcActionPropertyName = "MvcAction";
9+
810
public MvcActionNameEnricher()
9-
: this("MvcAction")
11+
: this(MvcActionPropertyName)
1012
{
1113
}
1214

src/SerilogWeb.Classic.Mvc/Classic/Mvc/Enrichers/MvcControllerNameEnricher.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/// </summary>
66
public class MvcControllerNameEnricher : BaseMvcContextInfoEnricher
77
{
8+
public const string MvcControllerPropertyName = "MvcController";
9+
810
public MvcControllerNameEnricher()
9-
: this("MvcController")
11+
: this(MvcControllerPropertyName)
1012
{
1113
}
1214

src/SerilogWeb.Classic.Mvc/Classic/Mvc/Enrichers/MvcRouteDataEnricher.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/// </summary>
66
public class MvcRouteDataEnricher : BaseMvcContextInfoEnricher
77
{
8+
public const string MvcRouteDataPropertyName = "MvcRouteData";
9+
810
public MvcRouteDataEnricher()
9-
: this("MvcRouteData")
11+
: this(MvcRouteDataPropertyName)
1012
{
1113
}
1214

src/SerilogWeb.Classic.Mvc/Classic/Mvc/Enrichers/MvcRouteTemplateEnricher.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/// </summary>
66
public class MvcRouteTemplateEnricher : BaseMvcContextInfoEnricher
77
{
8+
public const string MvcRouteTemplatePropertyName = "MvcRouteTemplate";
9+
810
public MvcRouteTemplateEnricher()
9-
: this("MvcRouteTemplate")
11+
: this(MvcRouteTemplatePropertyName)
1012
{
1113
}
1214

src/SerilogWeb.Classic.Mvc/SerilogWeb.Classic.Mvc.csproj

+9-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
4242
</Reference>
4343
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
44-
<HintPath>..\..\packages\Serilog.2.5.0\lib\net45\Serilog.dll</HintPath>
44+
<HintPath>..\..\packages\Serilog.2.6.0\lib\net45\Serilog.dll</HintPath>
45+
<Private>True</Private>
4546
</Reference>
46-
<Reference Include="SerilogWeb.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
47-
<HintPath>..\..\packages\SerilogWeb.Classic.3.0.20\lib\net45\SerilogWeb.Classic.dll</HintPath>
47+
<Reference Include="SerilogWeb.Classic, Version=4.2.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
48+
<HintPath>..\..\packages\SerilogWeb.Classic.4.2.41\lib\net45\SerilogWeb.Classic.dll</HintPath>
49+
<Private>True</Private>
4850
</Reference>
4951
<Reference Include="System" />
5052
<Reference Include="System.Core" />
@@ -88,9 +90,12 @@
8890
<Compile Include="Classic\Mvc\StoreMvcInfoInHttpContextActionFilter.cs" />
8991
<Compile Include="Classic\Mvc\MvcRequestInfoKey.cs" />
9092
<Compile Include="Properties\AssemblyInfo.cs" />
93+
<Compile Include="SerilogWebClassicMvcLoggerConfigurationExtensions.cs" />
9194
</ItemGroup>
9295
<ItemGroup>
93-
<None Include="packages.config" />
96+
<None Include="packages.config">
97+
<SubType>Designer</SubType>
98+
</None>
9499
<None Include="SerilogWeb.Classic.Mvc.nuspec" />
95100
<None Include="SerilogWeb.snk" />
96101
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Serilog.Configuration;
2+
using SerilogWeb.Classic.Mvc.Enrichers;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Serilog
10+
{
11+
/// <summary>
12+
/// Extends <see cref="LoggerConfiguration"/> to add enrichers for SerilogWeb.Classic.Mvc logging module
13+
/// </summary>
14+
public static class SerilogWebClassicMvcLoggerConfigurationExtensions
15+
{
16+
/// <summary>
17+
/// Enrich log events with the name of the current MVC action.
18+
/// </summary>
19+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
20+
/// <param name="propertyName">Name of the property to log.</param>
21+
/// <returns>Configuration object allowing method chaining.</returns>
22+
public static LoggerConfiguration WithMvcActionName(
23+
this LoggerEnrichmentConfiguration enrichmentConfiguration,
24+
string propertyName = MvcActionNameEnricher.MvcActionPropertyName)
25+
{
26+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
27+
return enrichmentConfiguration.With(new MvcActionNameEnricher(propertyName));
28+
}
29+
30+
/// <summary>
31+
/// Enrich log events with the controller name for the current MVC action.
32+
/// </summary>
33+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
34+
/// <param name="propertyName">Name of the property to log.</param>
35+
/// <returns>Configuration object allowing method chaining.</returns>
36+
public static LoggerConfiguration WithMvcControllerName(
37+
this LoggerEnrichmentConfiguration enrichmentConfiguration,
38+
string propertyName = MvcControllerNameEnricher.MvcControllerPropertyName)
39+
{
40+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
41+
return enrichmentConfiguration.With(new MvcControllerNameEnricher(propertyName));
42+
}
43+
44+
/// <summary>
45+
/// Enrich log events with the route data for the current MVC action.
46+
/// </summary>
47+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
48+
/// <param name="propertyName">Name of the property to log.</param>
49+
/// <returns>Configuration object allowing method chaining.</returns>
50+
public static LoggerConfiguration WithMvcRouteData(
51+
this LoggerEnrichmentConfiguration enrichmentConfiguration,
52+
string propertyName = MvcRouteDataEnricher.MvcRouteDataPropertyName)
53+
{
54+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
55+
return enrichmentConfiguration.With(new MvcRouteDataEnricher(propertyName));
56+
}
57+
58+
/// <summary>
59+
/// Enrich log events with the template for the current MVC action.
60+
/// </summary>
61+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
62+
/// <param name="propertyName">Name of the property to log.</param>
63+
/// <returns>Configuration object allowing method chaining.</returns>
64+
public static LoggerConfiguration WithMvcRouteTemplate(
65+
this LoggerEnrichmentConfiguration enrichmentConfiguration,
66+
string propertyName = MvcRouteTemplateEnricher.MvcRouteTemplatePropertyName)
67+
{
68+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
69+
return enrichmentConfiguration.With(new MvcRouteTemplateEnricher(propertyName));
70+
}
71+
}
72+
}

src/SerilogWeb.Classic.Mvc/packages.config

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
66
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
7-
<package id="Serilog" version="2.5.0" targetFramework="net45" />
8-
<package id="SerilogWeb.Classic" version="3.0.20" targetFramework="net45" />
7+
<package id="Serilog" version="2.6.0" targetFramework="net45" />
8+
<package id="SerilogWeb.Classic" version="4.2.41" targetFramework="net45" />
99
</packages>

test/SerilogWeb.Classic.Mvc.Test/Global.asax.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ public class MvcApplication : System.Web.HttpApplication
1717
protected void Application_Start()
1818
{
1919
Serilog.Log.Logger = new LoggerConfiguration()
20-
.Enrich.With<MvcControllerNameEnricher>()
21-
.Enrich.With<MvcRouteDataEnricher>()
22-
.Enrich.With<MvcRouteTemplateEnricher>()
23-
.Enrich.With<MvcActionNameEnricher>()
24-
.WriteTo.Trace(new JsonFormatter())
20+
.ReadFrom.AppSettings()
2521
.CreateLogger();
2622

2723
AreaRegistration.RegisterAllAreas();

test/SerilogWeb.Classic.Mvc.Test/SerilogWeb.Classic.Mvc.Test.csproj

+12-4
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@
6161
<HintPath>..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
64-
<HintPath>..\..\packages\Serilog.2.5.0\lib\net45\Serilog.dll</HintPath>
64+
<HintPath>..\..\packages\Serilog.2.6.0\lib\net45\Serilog.dll</HintPath>
65+
<Private>True</Private>
66+
</Reference>
67+
<Reference Include="Serilog.Settings.AppSettings, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
68+
<HintPath>..\..\packages\Serilog.Settings.AppSettings.2.1.2\lib\net45\Serilog.Settings.AppSettings.dll</HintPath>
69+
<Private>True</Private>
6570
</Reference>
6671
<Reference Include="Serilog.Sinks.Trace, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
6772
<HintPath>..\..\packages\Serilog.Sinks.Trace.2.1.0\lib\net45\Serilog.Sinks.Trace.dll</HintPath>
6873
</Reference>
69-
<Reference Include="SerilogWeb.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
70-
<HintPath>..\..\packages\SerilogWeb.Classic.3.0.20\lib\net45\SerilogWeb.Classic.dll</HintPath>
74+
<Reference Include="SerilogWeb.Classic, Version=4.2.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
75+
<HintPath>..\..\packages\SerilogWeb.Classic.4.2.41\lib\net45\SerilogWeb.Classic.dll</HintPath>
76+
<Private>True</Private>
7177
</Reference>
7278
<Reference Include="System" />
7379
<Reference Include="System.Data" />
@@ -175,7 +181,9 @@
175181
<Content Include="fonts\glyphicons-halflings-regular.woff" />
176182
<Content Include="fonts\glyphicons-halflings-regular.ttf" />
177183
<Content Include="fonts\glyphicons-halflings-regular.eot" />
178-
<None Include="packages.config" />
184+
<None Include="packages.config">
185+
<SubType>Designer</SubType>
186+
</None>
179187
<Content Include="Scripts\jquery-1.10.2.min.map" />
180188
</ItemGroup>
181189
<PropertyGroup>

test/SerilogWeb.Classic.Mvc.Test/Web.config

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
<add key="webpages:Enabled" value="false" />
1010
<add key="ClientValidationEnabled" value="true" />
1111
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
12+
13+
<add key="serilog:minimum-level" value="Debug" />
14+
<add key="serilog:using:Trace" value="Serilog.Sinks.Trace" />
15+
<add key="serilog:write-to:Trace"/>
16+
<add key="serilog:write-to:Trace.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception} {Properties:j}"/>
17+
18+
<add key="serilog:using:SerilogWeb.Classic.Mvc" value="SerilogWeb.Classic.Mvc"/>
19+
<add key="serilog:enrich:WithMvcActionName.propertyName" value="MvcActionNameTest"/>
20+
<add key="serilog:enrich:WithMvcControllerName.propertyName" value="MvcControllerNameTest"/>
21+
<add key="serilog:enrich:WithMvcRouteData.propertyName" value="MvcRouteDataTest"/>
22+
<add key="serilog:enrich:WithMvcRouteTemplate.propertyName" value="MvcRouteTemplateTest"/>
1223
</appSettings>
1324
<!--
1425
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

test/SerilogWeb.Classic.Mvc.Test/packages.config

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
<package id="Modernizr" version="2.6.2" targetFramework="net45" />
1616
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
1717
<package id="Respond" version="1.2.0" targetFramework="net45" />
18-
<package id="Serilog" version="2.5.0" targetFramework="net45" />
18+
<package id="Serilog" version="2.6.0" targetFramework="net45" />
19+
<package id="Serilog.Settings.AppSettings" version="2.1.2" targetFramework="net45" />
1920
<package id="Serilog.Sinks.Trace" version="2.1.0" targetFramework="net45" />
20-
<package id="SerilogWeb.Classic" version="3.0.20" targetFramework="net45" />
21+
<package id="SerilogWeb.Classic" version="4.2.41" targetFramework="net45" />
2122
<package id="WebGrease" version="1.5.2" targetFramework="net45" />
2223
</packages>

0 commit comments

Comments
 (0)