我正在使用 EntLib 5 创建滚动平面文件。这不使用滚动平面文件跟踪侦听器,而是使用平面文件跟踪侦听器并在代码中进行了一些修改。由于其固有设计存在问题,因此未使用滚动平面文件跟踪侦听器,因此不符合此处的要求。
这是配置部分:
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="./Logs/MyLog.log" formatter="Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp(local)}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<appSettings>
<add key="LogFolder" value="./Logs"/>
<add key="LogFileName" value="MyLog.log"/>
</appSettings>
<system.web>
然后我使用以下代码创建一个静态类来配置日志记录过程:
public static class LoggingHelper {
private static string date = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:yyyyMMdd}", DateTime.Now);
public static void SetLogFile(string logFileFolder,string logFileName) {
if (!string.IsNullOrEmpty(logFileName)) {
string strfileName = logFileFolder + "/" + date + logFileName;
LoggingHelper.SetTraceLogPath(strfileName, "FlatFile TraceListener", "General", "My Login System");
LoggingHelper.WriteLogFile("General", "Log file path " + logFileFolder + "/" + logFileName, " My Login System");
}
}
public static void SetTraceLogPath(string logFile, string logFileName, string category, string message) {
ConfigurationFileMap configFileMap = new ConfigurationFileMap();
configFileMap.MachineConfigFilename = "Web.config";
Configuration entLibConfig = WebConfigurationManager.OpenWebConfiguration("/ASP.NET_Logging");
LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
FlatFileTraceListenerData data = loggingSettings.TraceListeners.Get("Flat File Trace Listener") as FlatFileTraceListenerData;
data.FileName = logFile;
entLibConfig.Save();
LogEntry objLog = new LogEntry();
objLog.TimeStamp = System.DateTime.Now;
objLog.Categories.Add(category);
objLog.Message = message;
objLog.Priority = 1;
Logger.Write(objLog);
}
public static void WriteLogFile(string category, string msg, string title) {
try {
LogEntry le = new LogEntry();
le.TimeStamp = System.DateTime.Now;
le.Categories.Add(category);
le.Severity = TraceEventType.Information;
le.Priority = 1;
le.Message = msg;
le.Title = title;
le.Priority = 1;
Logger.Write(le);
} catch (LoggingException ex) {
LoggingHelper.WriteLogFile("General", "Error in writing log file " + ex.ToString(), "My Login System");
}
}}
在 Application_Start 方法中调用的 SetLogFile 是 Global.aspx 页面,如下所示:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
LoggingHelper.SetLogFile(ConfigurationSettings.AppSettings["LogFolder"].Trim (), ConfigurationSettings.AppSettings["LogFileName"].Trim());
LoggingHelper.WriteLogFile("General", "*** Application_Start ***", "");
}
然后我只需在需要记录的地方调用 WriteLogFile 方法,如下所示:
protected void btn_Page2_Click(object sender, EventArgs e) {
LoggingHelper.WriteLogFile("General", "btn_Page2_Click on Default.aspx", "");
Response.Redirect("SecongPage.aspx");
}
这工作正常,但对于一个小问题。第一次访问该网页时,它不会创建 Logfilename.log 格式的日志文件,它只是创建 MyLog.log 或 Web.config 中指定的任何名称,并在指定文件夹之外创建日志文件,在此案例“日志”,第一次。对于后续请求,它将在配置的文件夹中创建具有所需文件名的所需文件。这种模式每天重复,第一个请求的第二天,日志将写入昨天的文件,从下一个请求开始,创建一个具有今天日期的新文件,所有日志消息都将写入该文件。
我在我的代码中做了什么导致这种情况发生?
我们使用上面的代码,稍作改动,就可以毫无问题地登录我们的 WinForms 应用程序。
该网站当前未部署在 IIS 上。由于这是一项正在进行的工作,我们使用的是 VS 2008 的内置开发服务器。另一个值得关注的问题是代码在启动期间至少尝试更新一次 Web.config。不知道是不是这个方法!
这是一个非常简单的示例,有两页,第一页有一个按钮,点击后会跳转到第二页。我正在尝试每天实施日志文件,而 EntLib 附带的滚动平面文件不符合要求,因为它不会每天创建具有当天所需日期的文件。
问候。