logging - IIS 托管的 ASP.NET Core 应用程序中的控制台日志在哪里

标签 logging asp.net-core serilog

我使用优秀的 Serilog 库向 ASP.NET Core 应用程序添加了日志记录。我已经为 Sql Server 和 Literate 控制台添加了接收器。
sql server接收器当然工作得很好,但是,这有点尴尬,我不知道如何在控制台窗口中显示日志。
这是我尝试过的:

  1. 在发布的目录中,我运行了命令 dotnet run,但收到错误消息,指出 project.json 不存在。如果我在该文件所在的开发计算机上运行该命令,这当然有效。日志显示在打开的控制台中。
  2. 我还尝试直接运行exe,它说它正在监听:http://localhost:5000 ,但控制台窗口中没有显示任何日志记录。

代码如下:

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        ConfigureLogging();
    }

    private void ConfigureLogging()
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .WriteTo.LiterateConsole(LogEventLevel.Verbose)
            .WriteTo.RollingFile(".\\Logs\\log-{Date}.txt")
            .WriteTo.MSSqlServer(Configuration.GetConnectionString("Logging"), "Logs"/*, columnOptions: new ColumnOptions()*/)
            .CreateLogger();
    }

    public IConfigurationRoot Configuration { get; }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
    {
        app.Use(async (context, next) =>
        {
            await next();

            // If there's no available file and the request doesn't contain an extension, we're probably trying to access a page.
            // Rewrite request to use app root
            if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/"; // Put your Angular root page here 
                await next();
            }
        });

        loggerFactory
            .AddSerilog();

        // Ensure any buffered events are sent at shutdown
        appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseDefaultFiles();
        app.UseStaticFiles();

        //app.UseCors(builder => builder.AllowAnyOrigin());
        app.UseMvc();



        //app.UseHangfireDashboard();             // Will be available under http://localhost:5000/hangfire
        //app.UseHangfireServer();
    }

如何在控制台窗口中显示日志?

最佳答案

在 IIS 下,不会收集控制台日志。您需要使用 IIS 托管应用程序的其他接收器之一,例如您配置的滚动文件。

关于logging - IIS 托管的 ASP.NET Core 应用程序中的控制台日志在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636384/

相关文章:

c# - 如何测试asp.net core内置的Ilogger

c# - Serilog - 覆盖配置值

python - 记录来自 python-requests 模块的所有请求

logging - 如何记录我在 Emacs 中使用的所有命令?

c# - .NET 通用主机 : Prevent application crash on unhandled exception

asp.net-core - 在.Net Core Razor 页面中传递路由参数

javascript - .NET Core - 通过包装器异步加载 View 组件(客户端)

c# - Serilog SelfLog 到文件错误

.net-core - Serilog 将空白 CorrelationId 发送到 Seq 记录器

c# - 我可以从当前正在执行的函数中按程序获取参数名称/值吗?