ASP.NET 5 如何自动删除日志?
How to automatically delete logs in ASP.NET 5?
我有一个使用 ASP.NET 5 的 Web 应用程序。我刚刚熟悉新的内置日志记录机制 (Microsoft.Extensions.Logging
)。
我之前在其他应用程序中使用过NLog,NLog有一种机制可以在一段时间后自动删除日志文件。有没有什么办法可以在 ASP.NET 内置的日志记录中复制这种行为?例如,删除超过 7 天的日志文件?
我无法在其他地方找到关于此的文档...
我们仍然可以使用NLog。 The ASP.NET Logging repository says:
Community projects adapt Microsoft.Extensions.Logging for use with different back-ends.
包括 NLog。这里是NLog - provider for the NLog library,下面是一个简化的demo。您可以根据自己的目的对其进行调整。
目录结构
MyProject
nlog.config
project.json
Startup.cs
Startup.cs
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;
using NLog.Extensions.Logging;
public class Startup
{
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
//add NLog to aspnet5
loggerFactory.AddNLog();
//add NLog.Web (only needed if NLog.Web.ASPNET5 is needed)
app.AddNLogWeb();
//configure nlog.config in your project root
env.ConfigureNLog("./MyProject/nlog.config");
// we can also do this from a controller
// if we inject ILoggerFactory
var logger = loggerFactory.CreateLogger("NLog Demo");
logger.LogInformation("Hello from NLog");
}
}
project.json
{
"dependencies": {
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"NLog.Extensions.Logging": "1.0.0-rc1-final-2016-02-06",
"NLog.Web.ASPNET5": "4.2.1"
},
"frameworks": {
"dnx451": {}
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
}
}
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
archiveEvery="Minute"
archiveNumbering="Rolling"
maxArchiveFiles="1">
<extensions>
<add assembly="NLog.Web.ASPNET5"/>
</extensions>
<targets>
<target xsi:type="File" name="demo-file"
fileName="c:\temp\demo-file-${shortdate}.txt"
layout="${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="demo-file" />
</rules>
</nlog>
我有一个使用 ASP.NET 5 的 Web 应用程序。我刚刚熟悉新的内置日志记录机制 (Microsoft.Extensions.Logging
)。
我之前在其他应用程序中使用过NLog,NLog有一种机制可以在一段时间后自动删除日志文件。有没有什么办法可以在 ASP.NET 内置的日志记录中复制这种行为?例如,删除超过 7 天的日志文件?
我无法在其他地方找到关于此的文档...
我们仍然可以使用NLog。 The ASP.NET Logging repository says:
Community projects adapt Microsoft.Extensions.Logging for use with different back-ends.
包括 NLog。这里是NLog - provider for the NLog library,下面是一个简化的demo。您可以根据自己的目的对其进行调整。
目录结构
MyProject
nlog.config
project.json
Startup.cs
Startup.cs
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;
using NLog.Extensions.Logging;
public class Startup
{
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
//add NLog to aspnet5
loggerFactory.AddNLog();
//add NLog.Web (only needed if NLog.Web.ASPNET5 is needed)
app.AddNLogWeb();
//configure nlog.config in your project root
env.ConfigureNLog("./MyProject/nlog.config");
// we can also do this from a controller
// if we inject ILoggerFactory
var logger = loggerFactory.CreateLogger("NLog Demo");
logger.LogInformation("Hello from NLog");
}
}
project.json
{
"dependencies": {
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"NLog.Extensions.Logging": "1.0.0-rc1-final-2016-02-06",
"NLog.Web.ASPNET5": "4.2.1"
},
"frameworks": {
"dnx451": {}
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
}
}
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
archiveEvery="Minute"
archiveNumbering="Rolling"
maxArchiveFiles="1">
<extensions>
<add assembly="NLog.Web.ASPNET5"/>
</extensions>
<targets>
<target xsi:type="File" name="demo-file"
fileName="c:\temp\demo-file-${shortdate}.txt"
layout="${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="demo-file" />
</rules>
</nlog>