【发布时间】:2019-11-13 22:20:34
【问题描述】:
我在项目之前使用 nlog 和 log4net 系统来记录文件,而不是同时。没有问题。但是,我正在尝试记录文件。首先我尝试了 log4net,但它不起作用并且没有错误。然后我从我的项目中清除 log4net 代码。之后我安装 nlog 包。我再次尝试登录到文件。它不像 log4net 那样工作。 这是我的 log4net 代码:
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- log4net -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="E:\IDG.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
我将[assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)] 行写入assembly.Info
然后我写到 Program.cs 文件
class Program {
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args) {
log.Info("This is an informational message");
Console.ReadKey();
}
}
log4net 不工作后。我清理了 log4net 的那些代码和包。我试过nlog。 这是我的 nlog 代码:
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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="logFilePath" value="D:\IDG.log" />
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target name="logfile" xsi:type="File" fileName="${logFilePath}" layout="${longdate} LEVEL=${level:upperCase=true}: ${message}" keepFileOpen="true" />
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
</targets>
<rules>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Warn" writeTo="file"/>
</rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
还有我的 program.cs 文件:
private static Logger log = LogManager.GetCurrentClassLogger();
static void Main(string[] args) {
log.Trace("This is a trace message");
log.Debug("This is a debug message");
log.Info("This is an informational message");
log.Warn("This is a warning message");
log.Error("This is an error message");
log.Fatal("This is a fatal message");
Console.ReadKey();
}
}
这两个日志不起作用。我哪里错了?
【问题讨论】:
-
我猜 ´App.config` 在构建时被重命名,所以 log4net 找不到它。我猜你还没有配置
NLog.config文件属性:Copy If newer。但检查github.com/nlog/nlog/wiki/Logging-troubleshooting 总是个好主意