首页 > 代码库 > log4net 发布到生产环境不写日志的解决方法
log4net 发布到生产环境不写日志的解决方法
1、升级到log4net的最新版
PM下执行
Install-Package log4net
还是无法解决的,使用下面的方法
2、使用Nlog替换之,详见https://github.com/NLog/NLog/wiki/Tutorial
NLog使用方法比log4net更为简单,配置文件如下,如果成windows程序需将NLog.config自动复制到bin下面
1 <?xml version="1.0" encoding="utf-8" ?> 2 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" 5 autoReload="true" 6 throwExceptions="false" 7 internalLogLevel="Off" internalLogFile="F:\temp\20160907\NLog\log\nlog-internal.log"> 8 9 <!-- optional, add some variables10 https://github.com/nlog/NLog/wiki/Configuration-file#variables11 -->12 <variable name="myvar" value="myvalue"/>13 14 <!--15 See https://github.com/nlog/nlog/wiki/Configuration-file16 for information on customizing logging rules and outputs.17 -->18 <targets>19 20 <!--21 add your targets here22 See https://github.com/nlog/NLog/wiki/Targets for possible targets.23 See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.24 -->25 26 <!--27 Write events to a file with the date in the filename.28 <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"29 layout="${longdate} ${uppercase:${level}} ${message}" />30 -->31 <target name="logfile" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" />32 <target name="console" xsi:type="ColoredConsole" />33 </targets>34 35 <rules>36 <!-- add your logging rules here -->37 38 <!--39 Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"40 <logger name="*" minlevel="Debug" writeTo="f" />41 -->42 <logger name="*" minlevel="Debug" writeTo="logfile" />43 <logger name="*" minlevel="Info" writeTo="console" />44 </rules>45 </nlog>
调用代码:
1 Logger logger = LogManager.GetCurrentClassLogger(); 2 3 4 logger.Trace("Sample trace message"); 5 logger.Debug("Sample debug message"); 6 logger.Info("Sample informational message"); 7 logger.Warn("Sample warning message"); 8 logger.Error("Sample error message"); 9 logger.Fatal("Sample fatal error message");10 11 12 logger.Info("wilson测试 日志日期:{0}", DateTime.Now);
执行结果,非常漂亮的控制台输出:
log4net 发布到生产环境不写日志的解决方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。