控制台在哪里登录由IIS托管的asp.net核心应用程序

时间:2016-09-22 10:18:12

标签: logging asp.net-core serilog

我使用优秀的Serilog库将日志记录添加到asp.net核心应用程序中。我为Sql Server和Literate控制台都添加了接收器 sql server sink当然是完美的,但是,这有点令人尴尬,我不知道如何在控制台窗口中显示日志。
这是我尝试过的:

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

    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();
        }
    

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

1 个答案:

答案 0 :(得分:2)

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

相关问题