| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1234 人关注过本帖
标题:[求助]关于异常处理,如何以文本方式输出保存到相应的路径上?
只看楼主 加入收藏
zhzh
Rank: 2
等 级:新手上路
威 望:3
帖 子:377
专家分:0
注 册:2006-10-18
收藏
 问题点数:0 回复次数:4 
[求助]关于异常处理,如何以文本方式输出保存到相应的路径上?
关于异常处理,如何以文本方式输出保存到相应的路径上?
就是做一个异常处理的方法,把可能出现的所有异常,在catch捕获,调用那个方法
以文本方式输出,以当日日期为文本名,并保存到相应目录中.
给个详细的例子学习一下吧...试试了好多次,没成功.
小弟谢谢在先
搜索更多相关主题的帖子: 文本 路径 输出 保存 
2007-05-16 10:21
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
在catch中写一个
StreamWriter fileWriter = File.AppendText("log.txt");
fileWriter.WriteLine(e.manage);

[此贴子已经被作者于2007-5-16 10:33:26编辑过]


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-05-16 10:31
Kendy123456
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:62
帖 子:2720
专家分:0
注 册:2007-1-3
收藏
得分:0 

建议使用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


2007-05-16 10:45
zhzh
Rank: 2
等 级:新手上路
威 望:3
帖 子:377
专家分:0
注 册:2006-10-18
收藏
得分:0 
谢谢Kendy123456冰镇柠檬汁儿二位版主,
先收下了,试试看

Tomorrow is another day! my love..................
2007-05-16 13:14
zhzh
Rank: 2
等 级:新手上路
威 望:3
帖 子:377
专家分:0
注 册:2006-10-18
收藏
得分:0 

冰镇柠檬汁儿~~
怎么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


Tomorrow is another day! my love..................
2007-05-17 22:12
快速回复:[求助]关于异常处理,如何以文本方式输出保存到相应的路径上?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019519 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved