就是做一个异常处理的方法,把可能出现的所有异常,在catch捕获,调用那个方法
以文本方式输出,以当日日期为文本名,并保存到相应目录中.
给个详细的例子学习一下吧...试试了好多次,没成功.
小弟谢谢在先
建议使用Enterprise Library, 有专门针对异常的处理模块.
可以自己定制异常的类型, 可以包装异常的描述,可以配置异常的处理方法,指定记录到文件 到邮件 或者到系统日志.
给你一个例子, 这里用的是Enterprise Library 3, 用2的话 用法是差不多的.
先配置config文件:
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Global Policy">
<exceptionTypes>
<add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">
<exceptionHandlers>
<add name="Application Message Handler" type="ExceptionHandlingWithLoggingQuickStart.AppMessageExceptionHandler, ExceptionHandlingWithLoggingQuickStart"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
<add name="Log Only Policy">
<exceptionTypes>
<add name="Exception" type="System.Exception, mscorlib" postHandlingAction="None">
<exceptionHandlers>
<add
logCategory="Default Category"
eventId="100"
severity="Error"
title="Quick Start Application Exception"
priority="0"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
name="Logging Handler"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
<add name="Notify Policy">
<exceptionTypes>
<add name="BusinessLayerException" type="ExceptionHandlingQuickStart.BusinessLayer.BusinessLayerException, ExceptionHandlingQuickStart.BusinessLayer" postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add
logCategory="Default Category"
eventId="100"
severity="Error"
title="Enterprise Library Exception Handling"
priority="0"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
name="Logging Handler"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
/>
<add name="Replace Handler"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
exceptionMessage="An error occurred while processing your request. Please contact technical support using the following identifier: {handlingInstanceID}"
replaceExceptionType="System.ApplicationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<loggingConfiguration defaultCategory="Default Category" tracingEnabled="false">
<logFilters>
<add
name="Category"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
categoryFilterMode="AllowAllExceptDenied">
<categoryFilters />
</add>
<add
name="Priority"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
minimumPriority="0"
/>
</logFilters>
<categorySources>
<add
name="Default Category"
switchValue="All">
<listeners>
<add name="Event Log Destination" />
</listeners>
</add>
<add
name="Tracing"
switchValue="All">
<listeners>
<add name="Flat File Destination" />
</listeners>
</add>
</categorySources>
<specialSources>
<errors name="errors" switchValue="All">
<listeners>
<add name="Event Log Destination"/>
</listeners>
</errors>
</specialSources>
<listeners>
<add name="Event Log Destination"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
source ="Enterprise Library Logging"
formatter="Default Formatter"
/>
<add name="Flat File Destination"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
fileName ="trace.log"
/>
</listeners>
<formatters>
<add
name="Default Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
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}
Extended Properties: {dictionary({key} - {value}
)}"
/>
</formatters>
</loggingConfiguration>
VB.Net 调用的代码
Try
Process()
Catch ex As Exception
Dim logEntry As New LogEntry()
logEntry.Categories.Clear()
logEntry.Priority = 5
logEntry.Categories.Add("Tracing") ' 指定异常的处理方式是写到文件trace.log中
logEntry.Severity = TraceEventType.Error
logEntry.Message = ex.ToString
logEntry.EventId = 1
Logger.Write(logEntry)
Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
If (rethrow) Then ' 确定是否要把异常抛出给用户
Throw
End If
End Try
冰镇柠檬汁儿~~
怎么log.txt写不进出错信息啊!明明有异常了...
用Response.Write都能显示出现了异常的,还有这个log.txt是已经建立好的文本啊!
我要的是自动生成一个文本,出现异常就以当日日期为名字在指定目录里建立一个文本文件,
如果当天文本存在了就不用再建立了,接着添加异常记录..........
Kendy123456版主的是VB,能否做一下C#怎么写?看不懂这个
Try
Process()
Catch ex As Exception
Dim logEntry As New LogEntry()
logEntry.Categories.Clear()
logEntry.Priority = 5
logEntry.Categories.Add("Tracing") ' 指定异常的处理方式是写到文件trace.log中
logEntry.Severity = TraceEventType.Error
logEntry.Message = ex.ToString
logEntry.EventId = 1
Logger.Write(logEntry)
Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
If (rethrow) Then ' 确定是否要把异常抛出给用户
Throw
End If
End Try