使用Log4Net.AspNetCore和ILoggingBuilder的问题

时间:2019-03-29 04:37:44

标签: c# asp.net-core log4net log4net-configuration asp.net-core-2.2

ASP.NET Core 2.2内置提供程序似乎未包含文件提供程序。因此,我现在选择使用log4net。但是,当我尝试使用ILoggingBuilder类通过Microsoft.Extensions.Logging创建日志服务时,Log4Net似乎无法找到其配置文件。这是我的代码以及到目前为止我所做的事情的摘要:

Startup.cs

public class Startup
{
    private readonly ILogger _logger;

    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        Configuration = configuration;
        _logger = logger;

        _logger.LogInformation("Starting Container . . .");
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        _logger.LogInformation("Configuring DI Services . . .");

        services.AddLogging(logging =>
        {
            logging.AddConsole();
            logging.AddLog4Net();
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        _logger.LogInformation("Configuring Application . . .");

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Login}/{action=Index}/");
        });

        _logger.LogInformation("Web Application Startup Completed . . .");
    }
}

log4net.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <log4net>
    <appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.FileAppender">
      <file value="WebSiteDI.log" />
      <maximumFileSize value="500KB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="DebugAppender" />
      <appender-ref ref="RollingFile" />
    </root>
  </log4net>
</configuration>

我已经按照Log4Net.AspCoreNet Github页面(Link)的说明进行操作。但是,直到我在启动中添加以下内容后,该实现似乎才能够找到配置文件:

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

我知道有一个选项包,它允许我在appconfig.json中进行覆盖和其他配置(并允许我使用主机容器的环境切换功能),但这并不是必需的仍然取决于找到的log4net.config。

Github页面可能会过时还是我遗漏了什么?

0 个答案:

没有答案