放弃了以前写的日志记录类,改用log4net。
下载:http://archive.apache.org/dist/incubator/log4net/1.2.10/incubating-log4net-1.2.10.zip
引用:log4net.dll
配置如下:
建一个app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<logger name="AppLog">
<level value="INFO" />
<appender-ref ref="rollingFile" />
</logger>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="errLogs/Log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
</log4net>
</configuration>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<logger name="AppLog">
<level value="INFO" />
<appender-ref ref="rollingFile" />
</logger>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="errLogs/Log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
</log4net>
</configuration>
在 AssemblyInfo.vb 文件追加一句
<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="app.config", Watch:=True)>
修改 Global.asax
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' 应用程序启动时激发
log4net.Config.XmlConfigurator.Configure()
End Sub
' 应用程序启动时激发
log4net.Config.XmlConfigurator.Configure()
End Sub
调用
Dim log As log4net.ILog = log4net.LogManager.GetLogger("AppLog")
log.Debug("debug")
log.Info("info")
log.Warn("warn")
log.Error("error")
log.Debug("debug")
log.Info("info")
log.Warn("warn")
log.Error("error")
这样就生成了我想要的文件名格式:Log2008-06-27.log