ASP.NET项目 在Console窗口显示log4net日志
方法1,使用UdpAppender,参考http://www.codeproject.com/KB/aspnet/AspLog4netColorConsole.aspx
这种方法应当可以用于生产环境,log写法上注意一下性能问题就可以
方法2,直接使用WebDev.WebServer.exe
这种方法开发调试使用还是不错
a). Global.asax中配置log4net的代码
log4net.Config.XmlConfigurator.Configure(
new System.IO.FileInfo(System.IO.Path.Combine(
HttpContext.Current.Request.PhysicalApplicationPath, "log4net.config")));
b). log4net.config配置文件
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="log_erp" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_erp.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="%d %c - %m%n" />
</layout>
</appender>
<appender name="log_framework" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_framework.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="%d %c - %m%n" />
</layout>
</appender>
<appender name="log_web" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_web.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="[ %d %c ]=> %m%n" />
</layout>
</appender>
<appender name="log_console" type="log4net.Appender.ColoredConsoleAppender, log4net">
<mapping>
<level value="FATAL" />
<foreColor value="Red" />
<backColor value="White" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Cyan" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="[%p] %message%newline" />
</layout>
</appender>
<root>
<level value="WARN" />
<appender-ref ref="log_erp" />
</root>
<logger name="Magic.Framework" additivity="false">
<level value="INFO" />
<appender-ref ref="log_framework" />
<appender-ref ref="log_console" />
</logger>
<logger name="ERP.Web" additivity="false">
<level value="INFO" />
<appender-ref ref="log_web" />
</logger>
<logger name="ERP.Web.Inventory" additivity="false">
<level value="DEBUG" />
<appender-ref ref="log_web" />
<appender-ref ref="log_console" />
</logger>
<logger name="Magic.ERP" additivity="false">
<level value="DEBUG" />
<appender-ref ref="log_erp" />
<appender-ref ref="log_console" />
</logger>
</log4net>
</configuration>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="log_erp" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_erp.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="%d %c - %m%n" />
</layout>
</appender>
<appender name="log_framework" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_framework.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="%d %c - %m%n" />
</layout>
</appender>
<appender name="log_web" type="log4net.Appender.RollingFileAppender, log4net">
<param name="File" value="log\log_web.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="[ %d %c ]=> %m%n" />
</layout>
</appender>
<appender name="log_console" type="log4net.Appender.ColoredConsoleAppender, log4net">
<mapping>
<level value="FATAL" />
<foreColor value="Red" />
<backColor value="White" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Cyan" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="*********************************** " />
<param name="Footer" value=" " />
<param name="ConversionPattern" value="[%p] %message%newline" />
</layout>
</appender>
<root>
<level value="WARN" />
<appender-ref ref="log_erp" />
</root>
<logger name="Magic.Framework" additivity="false">
<level value="INFO" />
<appender-ref ref="log_framework" />
<appender-ref ref="log_console" />
</logger>
<logger name="ERP.Web" additivity="false">
<level value="INFO" />
<appender-ref ref="log_web" />
</logger>
<logger name="ERP.Web.Inventory" additivity="false">
<level value="DEBUG" />
<appender-ref ref="log_web" />
<appender-ref ref="log_console" />
</logger>
<logger name="Magic.ERP" additivity="false">
<level value="DEBUG" />
<appender-ref ref="log_erp" />
<appender-ref ref="log_console" />
</logger>
</log4net>
</configuration>
c). 写一个批处理文件debug.bat
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE /port:8101 /path:"D:\Work\Blooming\S2\Magic.ERP\Web" /vpath:"/"
运行debug.bat,访问http://localhost:8101/
如果站点默认页面不是default.aspx,写一个default.aspx调转到默认页面比较方便
效果: