我使用优秀的Serilog库将日志记录添加到asp.net核心应用程序中。我为Sql Server和Literate控制台都添加了接收器
sql server sink当然是完美的,但是,这有点令人尴尬,我不知道如何在控制台窗口中显示日志。
这是我尝试过的:
以下是代码:
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();
}
如何在控制台窗口中显示日志?
答案 0 :(得分:2)
在IIS下,不会收集控制台日志。您需要为IIS托管应用程序使用其他一个接收器,例如您已配置的滚动文件。