我刚刚继承了一个项目,前一个团队正在使用nLog(这不是我熟悉的记录器)。日志有一个令人讨厌的问题,日志文件的记录器名称部分被截断为40个字符。我无法理解它导致了什么,这个输出使得日志文件很难处理/调试问题。
项目正在以编程方式配置日志目标(FileTarget),记录器布局为:
${longdate}\t[${threadid}]\t${level:uppercase=true}\t${logger}\t${mdc:item=identifier}\t${message}\t${exception:format=tostring}
日志文件输出:
# Cleaned up for brevity
2017-08-15 ... [1] INFO MyProject.Some.Namespace.TruncatedFullNa Resolve: ClientDevice=Desktop, ClientOS=Unknown
2017-08-15 ... [1] DEBUG MyProject.Some.Namespace.WhyCantISeeTheN System code from context: 0x004F
2017-08-15 ... [1] DEBUG MyProject.Some.Namespace.SubNameSpace.Wh Resolve Starting
2017-08-15 ... [1] ERROR MyProject.Some.Namespace.SubNameSpace.Wh Resolve failed - ....
记录器创建(每个类):
private static readonly NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
的Web.config:
<nlog>
<extensions>
<add assembly="Custom.Logging" />
</extensions>
<targets>
<target name="AggregateLevelRollingFile" type="AggregateLevelRollingFile" />
<target name="LevelRollingFile" type="LevelRollingFile" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="AggregateLevelRollingFile,LevelRollingFile" />
</rules>
</nlog>
更新 - 添加自定义目标代码
public AggregateLevelRollingFile()
{
var baseDir = ResolveLogDir();
var applicationName = AppContext.GetName();
FileName = string.Format("{0}\\{1}\\{2}", baseDir, applicationName,"{shortdate}.All.log");
ArchiveFileName = string.Format("{0}\\{1}\\{2}", baseDir, applicationName, "${shortdate}_{##}.All.log");
MaxArchiveFiles = 1000000;
ArchiveAboveSize = 5 * 1024 * 1024;
Layout =
new LayoutWithHeaderAndFooter
{
Header = new SimpleLayout("Time\tThread\tLevel\tLogger\tMessage\tException"),
Layout = new SimpleLayout("${longdate}\t[${threadid}]\t${level:uppercase=true}\t${logger}\t${mdc:item=identifier}\t${message}\t${exception:format=tostring}")
};
}