本文介绍了如何在ASP.NET Web表单使用Autofac注入NLOG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web表单的项目,最近我一直在通过转换到Autofac使用依赖注入。它具有所有进展顺利,这是直到我试图注入我NLOG实例。我不能工作了如何执行静态GetCurrentClassLogger()方法是等效的与物业调用注入Logger对象。

I have an ASP.NET Webforms project that I've been recently converting to use Dependency Injection via Autofac. It has all been going well, that is until I tried to inject my NLog instances. I can't work out how to perform the equivalent of the static GetCurrentClassLogger() method calls with the property injected Logger object.

下面是我的示例code能说明问题:

Here is my sample code illustrating the problem:

的Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Autofac;
using Autofac.Integration.Web;

public class Global : HttpApplication, IContainerProviderAccessor
{
  private static IContainerProvider containerProvider;

  public IContainerProvider ContainerProvider
  {
    get { return containerProvider; }
  }

  void Application_Start(object sender, EventArgs e)
  {
    var builder = new ContainerBuilder();
    Bootstrapper.RegisterInstances(builder);
    containerProvider = new ContainerProvider(builder.Build());
  }
}

Bootstrapper.cs

using Autofac;

public static class Bootstrapper
{
  public static void RegisterInstances(ContainerBuilder builder)
  {
    builder.RegisterType<Logger>().As<ILogger>().InstancePerRequest();
    builder.RegisterType<MyTestModule>().As<IMyTestModule>().InstancePerRequest();
  }
}

MyTestModule.cs

using System;

public class MyTestModule : IMyTestModule
{
   // was previously: protected static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

  private ILogger _logger;

  public MyTestModule(ILogger logger)
  {
    _logger = logger;
  }

  public void DoSomethingBad()
  {
    try
    {
        int z = 5 / 0;
    }
    catch (Exception ex)
    {
        _logger.Error("Error!", ex);
    }
  }
}

public interface IMyTestModule
{
  void DoSomethingBad();
}

PageBase.cs

public abstract class PageBase : Page
{
  // was previously: protected static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

  public ILogger Logger { get; set; }

  public IMyTestModule MyTestModuleInstance { get; set; }
}

Default.aspx的

<%@ Page Title="Home" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html>
  <head></head>
  <body></body>
</html>

Default.aspx.cs

using System;

public partial class _Default : PageBase
{
  protected void Page_Load(object sender, EventArgs e)
  {
    MyTestModuleInstance.DoSomethingBad();
  }
}

您可以看到的 PageBase.cs MyTestModule.cs 我注释掉previous静态调用创建NLOG例如:

You can see in PageBase.cs and MyTestModule.cs I have commented out the previous static calls to create the NLog instance:

protected static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

他们现在只是在Autofac依赖性取代注入属性是这样的:

They are now simply replaced with the Autofac dependency injected property like this:

public ILogger Logger { get; set; }

这工作在这么多的Logger对象被初始化到记录器的实例。

This works in so much that the Logger object gets initialised to an instance of the logger.

我如何执行相当于 LogManager.GetCurrentClassLogger()

有什么我需要NLOG模块做实现这一目标?如果我没能找到任何具体的例子。任何帮助将是非常美联社preciated!

Is there something I need to do with NLog modules to achieve this? If there is I've not been able to find any specific examples. Any help would be much appreciated!

推荐答案

重写 AttachTocomponentRegistaration 你的模块:

public class AutofacLogInjectionModule : Module
{
    protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
    {
        registration.Preparing += OnComponentPreparing;
    }

    static void OnComponentPreparing(object sender, PreparingEventArgs e)
    {
        e.Parameters = e.Parameters.Union(new[]
    {
        new ResolvedParameter((p, i) => p.ParameterType == typeof(ILogger), (p, i) => LogManager.GetCurrentClassLogger())
    });
    }

}

而在你的引导注册此模块:

And Register this module in your bootstrap:

builder.RegisterModule(new AutofacLogInjectionModule());

这篇关于如何在ASP.NET Web表单使用Autofac注入NLOG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:38