让log4net输出到文件,只花了一分钟时间,但为了同时能在Visual studio的output窗口也同步输出,花了很多时间。一般都认为output窗口就是console,然而并不是那样的。
引用这个帖子:https://stackoverflow.com/questions/20888674/log4net-outputting-file-but-not-to-debug-window
This was driving me crazy! I was able to setup logging in node.js with log4js < 5 mins, trying to use log4net took so long because I thought the vs debug window WAS the console, so I was mistakenly using the ConsoleAppender as well once I figured out the janky xml config for log4net.
– Jacob McKay
所以答案就是不要用ConsoleAppender 而是要用TraceAppender。
再贴一下原贴的config:
<log4net> <!-- This writes the log information to the console window. It only logs events that are at least at the INFO level (which would mean that DEBUG events are not captured. --> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="test"/> </filter> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="error"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/> </layout> </appender> <!-- This stores information in the logfile.txt file. It only captures log events that contain the key word test or error. --> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:\TOPS\LOGFILES\logfile.txt"/> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="5"/> <maximumFileSize value="10MB"/> <staticLogFileName value="true"/> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="test"/> </filter> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="error"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="RollingFileAppender"/> <appender-ref ref="TraceAppender"/> </root> </log4net>
标签:log4net,输出,窗口,ConsoleAppender,console,VS,Output,was From: https://www.cnblogs.com/crazyghostvon/p/log4nettrace.html