【问题标题】:How can I implement iLoggerFactory on my server IIS?如何在我的服务器 IIS 上实现 iLoggerFactory?
【发布时间】:2019-08-24 17:35:51
【问题描述】:

我在我的 .Net Core API 上实现 iLoggerFactory,步骤:

  • 创建我的文件夹“日志”
  • 在我的“日志”文件夹中实现日志(在 startup.ts 中)

    public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
    {
        // log implementation
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddEventSourceLogger();
        loggerFactory.AddFile("logs/app-{Date}.txt");
    
        app.UseStaticFiles();
        app.UseCors("AnyOrigin");
        app.UseSwagger();
    
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebToolAPI");
            c.RoutePrefix = string.Empty;
        });
        app.UseMvc();
    }
    
  • 我创建了一些看起来像这样的登录控制器:

    _log.LogError("\r\n -- \r\n Datetime : {0} \r\n ERROR : id'" + id + "' not found \r\n --", DateTime.Now);
    

当我在我的本地机器上测试它时它可以工作,但是当我将我的代码放入我的 IIS 服务器时它就不起作用了。它不会在我的日志文件夹中创建日志。

【问题讨论】:

  • 您能分享一下您是如何定义 ILoggerFactory AddFile 方法的吗?你用过哪个库?或者你建立一个自定义的 ILoggerFactory?如果您可以发布更多详细信息,我们将更容易重现问题并找到解决方案。
  • 您好,我有人帮助我,我会为有问题的人发布解决方案。

标签: iis logging asp.net-core-2.1


【解决方案1】:

我解决了这个问题。

在 Startup.cs 中:

public class Startup
{
    string LogFilePath;
    public Startup(IHostingEnvironment env)
    {
        // location of the logs
        var exePath = Assembly.GetEntryAssembly().Location.ToString();
        var directoryPath = Path.GetDirectoryName(exePath);
        LogFilePath = Path.Combine(directoryPath + "\\logs\\"); // you can replace "logs" by an other folder name
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
    {
        loggerFactory.AddDebug();
        loggerFactory.AddFile(LogFilePath + "api-{Date}.txt",LogLevel.Debug,null,false,null,30); // you can replace "api" by an other file name for your log
    }
}

在 Program.cs 中:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseKestrel()
        .UseIISIntegration()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2021-06-01
    • 2012-11-28
    • 2018-01-05
    • 1970-01-01
    • 2012-01-18
    • 2011-11-08
    相关资源
    最近更新 更多