Skip to main content
added 300 characters in body
Source Link
alek kowalczyk
  • 5k
  • 3
  • 29
  • 57

Worked it out, by using two options:

  1. ILoggerProvider Implement your own ILoggerProvider and ILogger from the namespace Microsoft.Framework.Logging Then attach it to the MVC Framework in Startup.cs add following code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { loggerfactory.AddProvider(new YourCustomProvider()); } But this above option, seems to only call the Write function of the ILogger on MVC specific events, routing related and so on, it wasn't called when I threw exceptions on my lower layers, so the second option worked out:

  2. Global Filter Register your own ActionFilterAttribute implementation in Startup.cs:

    public void ConfigureServices(IServiceCollection services) { services.AddMvc().Configure(options => { options.Filters.Add(new YourCustomFilter()); }); } It's important, that the custom filter class implements the IExceptionFilter interace:

    public class YourCustomFilter : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext context) { ///logic... } } (EDIT:) And then in the Startup class we add the filter:

     public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(new YourCustomFilter()); }); } 

Worked it out, by using two options:

  1. ILoggerProvider Implement your own ILoggerProvider and ILogger from the namespace Microsoft.Framework.Logging Then attach it to the MVC Framework in Startup.cs add following code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { loggerfactory.AddProvider(new YourCustomProvider()); } But this above option, seems to only call the Write function of the ILogger on MVC specific events, routing related and so on, it wasn't called when I threw exceptions on my lower layers, so the second option worked out:

  2. Global Filter Register your own ActionFilterAttribute implementation in Startup.cs:

    public void ConfigureServices(IServiceCollection services) { services.AddMvc().Configure(options => { options.Filters.Add(new YourCustomFilter()); }); } It's important, that the custom filter class implements the IExceptionFilter interace:

    public class YourCustomFilter : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext context) { ///logic... } }

Worked it out, by using two options:

  1. ILoggerProvider Implement your own ILoggerProvider and ILogger from the namespace Microsoft.Framework.Logging Then attach it to the MVC Framework in Startup.cs add following code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { loggerfactory.AddProvider(new YourCustomProvider()); } But this above option, seems to only call the Write function of the ILogger on MVC specific events, routing related and so on, it wasn't called when I threw exceptions on my lower layers, so the second option worked out:

  2. Global Filter Register your own ActionFilterAttribute implementation in Startup.cs:

    public void ConfigureServices(IServiceCollection services) { services.AddMvc().Configure(options => { options.Filters.Add(new YourCustomFilter()); }); } It's important, that the custom filter class implements the IExceptionFilter interace:

    public class YourCustomFilter : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext context) { ///logic... } } (EDIT:) And then in the Startup class we add the filter:

     public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(new YourCustomFilter()); }); } 
Source Link
alek kowalczyk
  • 5k
  • 3
  • 29
  • 57

Worked it out, by using two options:

  1. ILoggerProvider Implement your own ILoggerProvider and ILogger from the namespace Microsoft.Framework.Logging Then attach it to the MVC Framework in Startup.cs add following code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { loggerfactory.AddProvider(new YourCustomProvider()); } But this above option, seems to only call the Write function of the ILogger on MVC specific events, routing related and so on, it wasn't called when I threw exceptions on my lower layers, so the second option worked out:

  2. Global Filter Register your own ActionFilterAttribute implementation in Startup.cs:

    public void ConfigureServices(IServiceCollection services) { services.AddMvc().Configure(options => { options.Filters.Add(new YourCustomFilter()); }); } It's important, that the custom filter class implements the IExceptionFilter interace:

    public class YourCustomFilter : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext context) { ///logic... } }