在我的Web应用程序中,我正在尝试创建用于记录错误和异常的日志文件,但是当我运行应用程序时,日志文件未在我的解决方案文件夹或bin文件夹中创建。
我使用了以下代码。请帮帮我,我遇到了这个问题,请提前谢谢。
使用的名称空间
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
.cs文件
public int GetdeskKMasterRecordsCount(string CircleId, string StoreId)
{
try
{
throw new Exception("this is normal exception");
}
catch (Exception ex)
{
BindLog(ex);
return 0;
}
}
public static void BindLog(Exception ex)
{
if (ex == null) return;
Logger.Write(LogInformation(1, DateTime.Now, ex.Message, " "));
}
public static LogEntry LogInformation(int eventId, DateTime timeStamp, string message, string documentName)
{
LogEntry logEntryObject = new LogEntry();
logEntryObject.EventId = eventId;
logEntryObject.Title = documentName;
logEntryObject.TimeStamp = timeStamp;
logEntryObject.MachineName = System.Environment.MachineName;
logEntryObject.Message = message;
logEntryObject.Categories.Add("Exception");
return logEntryObject;
}
网络配置文件
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="recordingWindowGroup" type="Vodafone.DMS.BAL.WindowConfigurationHandler, Vodafone.DMS.BAL"/>
<section name="defaultParamGroup" type="Vodafone.DMS.BAL.DefaultParamConfiguration, Vodafone.DMS.BAL"/>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add name="Rolling Flat File Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="Vodafone.DMS.log.exclude" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment"
rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/>
</listeners>
<formatters>
<add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/>
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Rolling Flat File Trace Listener"/>
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Rolling Flat File Trace Listener"/>
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="Rolling Flat File Trace Listener"/>
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Rolling Flat File Trace Listener"/>
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
答案 0 :(得分:4)
对于企业库version 5.0 and above
,Microsoft发布了一个企业库配置tool,允许您在configuration file
中直观地更改配置。您也可以从NuGet安装此工具,可以从visual studio Tools >> Library Package Manager >> Manage NuGet Packages for solution
中访问该工具,并添加相应的 EntLib Application Block
。您可以从here找到指南。
使用该工具我已经生成了一个配置文件,如下所示:
将其放在Web.Config或App.Config文件中
<loggingConfiguration name="loggingConfiguration" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="RollingFlatFile.log"
footer="---------------------------" formatter="Text Formatter"
header="---------------------------" rollFileExistsBehavior="Increment"
rollInterval="Week" rollSizeKB="20000" timeStampPattern="yyyy-MM-dd hh:mm:ss"
maxArchivedFiles="7" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
filter="All" />
</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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
我建议您不要在应用程序的bin目录中生成日志文件,而应通过将 fileName="RollingFlatFile.log"
的值更改为来指定物理位置fileName="c:\logs\RollingFlatFile.log"
强>
答案 1 :(得分:0)
您需要在LogEntry上设置一个类别,以匹配配置文件中的类别源(您发布的配置中的“常规”)。如果您计划记录异常,我建议您查看异常处理应用程序块,而不是编写代码来自行处理创建LogEntry。
答案 2 :(得分:-2)
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
Logger.Write("btnStartProcess_Click");
Logger.Reset();