我已使用IIS 8在Windows Server 2012上部署了Asp.net Core 2 Web应用程序。
我无法记录"信息"到日志文件,它只记录"警告"。我在此链接后创建了一个自定义记录器:
它在我的localhost上部署时执行预期的工作。我也启用了权限,因此有一些日志记录可以正常工作,例如stdout logs。
但是,为了保存记录,我还要存储其他重要日志。
下面是我的日志文件的屏幕截图
代码片段:在StartUp.cs中配置方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory )
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
loggerFactory.AddProvider(new CustomLoggerProvider(new CustomLoggerProviderConfiguration
{
LogLevel = LogLevel.Information
},env.ContentRootPath));
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=test}/{action=test}/{id?}");
});
}
代码段:实际创建文件的函数
private void WriteToLog(string message)
{
////When Date change create new file.
string filePath = _hostingPath + "\\test\\" + "Web_" + DateTime.Today.ToString("yyyy_MM_dd") + ".txt";
FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Write);
using (StreamWriter streamWriter = new StreamWriter(fs))
{
streamWriter.WriteLine(message);
streamWriter.Close();
}
}
另外,我知道将日志存储在服务器上并不是一个好习惯,因为它需要超时空间。有没有其他方法可以建议存储日志请告诉我们?
此外,在本期LogLevel"信息"没有在IIS上显示。