Skip to content
Permalink
Browse files

Added ILogger injection via Autofac, with request correlation via Ser…

…ilogWeb.Classic
  • Loading branch information...
Adam Grande
Adam Grande committed Oct 31, 2016
1 parent d4a5145 commit a57139df2ead6f1b8199173f5128479b4a0958b2
@@ -0,0 +1,31 @@
using System.Configuration;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using AutofacSerilogIntegration;
using Serilog;
using SerilogWeb.Classic.Enrichers;

namespace SerilogPoc
{
public static class IoCConfig
{
private static IContainer Container { get; set; }

public static void Configure()
{
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.With<HttpRequestIdEnricher>()
.WriteTo.Seq(ConfigurationManager.AppSettings["SeqUrl"])
.CreateLogger();

var builder = new ContainerBuilder();
builder.RegisterLogger(logger);
builder.RegisterControllers(typeof(WebApiApplication).Assembly);

Container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(Container));
}
}
}

This file was deleted.

Oops, something went wrong.
@@ -1,19 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc;
using Serilog;

namespace SerilogPoc.Controllers
{
public class HomeController : Controller
{
private readonly ILogger _logger;

public HomeController(ILogger logger)
{
_logger = logger;
}
public ActionResult Index()
{
Log.Information("Invoking Index() Action");
_logger.Information("Invoking Index() Action with injected logger");
ViewBag.Title = "Home Page";
Log.Information("Finished Index() Action");
_logger.Information("Finished Index() Action with injected logger");
return View();
}
}
@@ -11,16 +11,14 @@ public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
SerilogConfig.Configure();
Log.Information("Configuration Application");
IoCConfig.Configure();

AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Log.Information("Application Configured");
}
}
}
@@ -48,6 +48,14 @@
<HintPath>..\packages\Autofac.4.1.1\lib\net45\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Autofac.Integration.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.Mvc5.4.0.0\lib\net451\Autofac.Integration.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="AutofacSerilogIntegration, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutofacSerilogIntegration.1.0.12\lib\portable45-net45+win8+wp8+wpa81\AutofacSerilogIntegration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.Agent.Intercept, Version=1.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.Agent.Intercept.1.2.1\lib\net45\Microsoft.AI.Agent.Intercept.dll</HintPath>
<Private>True</Private>
@@ -105,6 +113,10 @@
<HintPath>..\packages\Serilog.Sinks.Seq.3.0.1\lib\net45\Serilog.Sinks.Seq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SerilogWeb.Classic, Version=2.0.0.0, Culture=neutral, PublicKeyToken=9462ddd55fbc0e7f, processorArchitecture=MSIL">
<HintPath>..\packages\SerilogWeb.Classic.2.0.10\lib\net45\SerilogWeb.Classic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
@@ -177,14 +189,13 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\IoCConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\SerilogConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Global.asax.cs">
@@ -229,7 +240,6 @@
<Content Include="fonts\glyphicons-halflings-regular.ttf" />
<Content Include="fonts\glyphicons-halflings-regular.eot" />
<Content Include="packages.config" />
<None Include="Project_Readme.html" />
<Content Include="Scripts\jquery-1.10.2.min.map" />
</ItemGroup>
<PropertyGroup>
@@ -51,7 +51,15 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Serilog" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
@@ -2,6 +2,8 @@
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net462" />
<package id="Autofac" version="4.1.1" targetFramework="net462" />
<package id="Autofac.Mvc5" version="4.0.0" targetFramework="net462" />
<package id="AutofacSerilogIntegration" version="1.0.12" targetFramework="net462" />
<package id="bootstrap" version="3.0.0" targetFramework="net462" />
<package id="jQuery" version="1.10.2" targetFramework="net462" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net462" />
@@ -25,5 +27,6 @@
<package id="Serilog.Sinks.PeriodicBatching" version="2.0.0" targetFramework="net462" />
<package id="Serilog.Sinks.RollingFile" version="3.0.0" targetFramework="net462" />
<package id="Serilog.Sinks.Seq" version="3.0.1" targetFramework="net462" />
<package id="SerilogWeb.Classic" version="2.0.10" targetFramework="net462" />
<package id="WebGrease" version="1.5.2" targetFramework="net462" />
</packages>

0 comments on commit a57139d

Please sign in to comment.
You can’t perform that action at this time.