记录(例外等) [英] Logging (exceptions etc)

查看:66
本文介绍了记录(例外等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在进入.NET中的Trace功能,使用它在dll中提供一些需要的日志记录在我们正在进行的项目中。然而,

是一个新手,我想知道一些更有经验的伐木工人是否可以提供一些关于如何以简单而灵活的方式登录的想法。对于

实例,我希望代码尽可能通过Trace

语句整洁。


作为基本日志记录功能的例子,我已经提出了一些

替代方法来记录对象中发生的异常:


1)包装投掷方法中的异常,它可以在抛出实际异常之前处理任何

日志记录。


2)处理我自己的异常层次结构的构造函数中的日志记录。

这使得日志记录在代码中更加透明,但是对于标准.NET异常(例如ArgumentNullException)不会起作用(我必须要子类型

it)。


3)使用装饰器对象包装原始对象,捕获所有异常,

写入日志,并且重新抛出。


非常感谢任何和所有想法/评论!


问候,

Einar


-

如果它是如此,它可能是;如果是这样,那就是;但是,因为它不是,

它不是。这是'逻辑' - Lewis Carroll

Hi,

I''m getting into the Trace-functionality in .NET, using it to provide some
much-needed logging across dlls in the project we''re working on. However,
being a newbie, I''m wondering if some more experienced loggers can provide
me with some ideas as to how to log in a simple yet flexible manner. For
instance, I''d like the code to be as uncluttered as possible by Trace
statements.

As an example of basic logging functionality, I''ve come up with some
alternatives for logging exceptions that occur in an object:

1) Wrap the throwing of an exception in a method, which can handle any
logging before throwing the actual exception.

2) Handle logging in the constructors of my own hierarchy of exceptions.
This makes the logging more transparent in the code, but doesn''t work for
standard .NET exceptions such as ArgumentNullException (I''d have to subtype
it).

3) Use a decorator object to wrap the original object, catch all exceptions,
write to log, and rethrow.

Any and all thoughts/comments are much appreciated!

Regards,
Einar

--
"If it was so, it might be; and if it were so, it would be; but as it isn''t,
it ain''t. That''s logic" -- Lewis Carroll

推荐答案




您是否看过MSDN上的异常管理应用程序块? ?


异常管理应用程序块提供了一个简单但可扩展的框架来处理异常。使用单行应用程序代码,您可以轻松地将异常信息记录到事件日志中,或通过创建自己的组件来扩展它,将异常详细信息记录到其他数据源或通知操作员,而不会影响您的应用程序代码。异常管理应用程序块可以很容易地用作您自己的.NET应用程序中的构建块。


[ http://msdn.microsoft.com/library/de ... ml / emab-rm.asp ]

问候,

Saurabh Nandu

Hi,

Have you looked at the Exception Mangement Application Block at MSDN ?

The Exception Management Application Block provides a simple yet extensible framework for handling exceptions. With a single line of application code you can easily log exception information to the Event Log or extend it by creating your own components that log exception details to other data sources or notify operators, without affecting your application code. The Exception Management Application Block can easily be used as a building block in your own .NET application.

[ http://msdn.microsoft.com/library/de...ml/emab-rm.asp ]
Regards,
Saurabh Nandu





感谢您的提示。我实际上刚才看过它,但那时候我没有像我现在那样真正进入异常管理。我会

重新审视它,谢谢。


问候,

Einar


" Saurabh Nandu" <萨********** @ discussions.microsoft.com>在消息中写道

新闻:F8 ********************************** @ microsof t.com ...
Hi,

Thanks for the tip. I actually did take a look at it a while ago, but at
that time I wasn''t really into exception management the way I am now. I''ll
take a fresh look at it, thanks.

Regards,
Einar

"Saurabh Nandu" <Sa**********@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
您好,

您是否看过MSDN上的异常管理应用程序块?

异常管理应用程序块提供一个简单但
可扩展的框架,用于处理异常。只需一行

应用程序代码,您就可以轻松地将异常信息记录到事件日志

或通过创建自己的组件来扩展它,以便将异常详细信息记录到

其他数据源或通知运营商,不影响您的申请

代码。异常管理应用程序块可以很容易地用作您自己的.NET应用程序中的构建块。
[
http://msdn.microsoft.com/library/ de ... ml / emab-rm.asp ]

问候,
Saurabh Nandu
Hi,

Have you looked at the Exception Mangement Application Block at MSDN ?

The Exception Management Application Block provides a simple yet extensible framework for handling exceptions. With a single line of
application code you can easily log exception information to the Event Log
or extend it by creating your own components that log exception details to
other data sources or notify operators, without affecting your application
code. The Exception Management Application Block can easily be used as a
building block in your own .NET application.
[ http://msdn.microsoft.com/library/de...ml/emab-rm.asp ]

Regards,
Saurabh Nandu



正如其他人写的那样,从MS Exception Management块开始。你可以

需要修改/扩展它,但这是一个非常好的起点。


我使用的策略可以归结为...


如果这是我自己的代码抛出的异常,那么如果它是第一次抛出异常,那么我会记录异常。如果它正在传播一个电话

堆栈(当一个catch块捕获它时会发生这种情况,将它包装在另一个

异常中,并重新抛出它),我这样做不记录它。我通过检查InnerException属性

值来检测异常对象的

构造函数。


如果抛出异常通过外部模块然后捕获代码必须

如果有必要,手动调用异常发布方法。


如果异常越过边界我记录它(也许再次) - 这个

确保异常数据永远不会丢失,以防

生成异常的原始站点没有记录它。你必须定义边界

是什么 - 它可以是appdomain,机器,服务器,模块等。


另外要寻找的是吞噬了一个例外。虽然

这被认为是坏的。尽管如此,它仍然会发生,有时合理地说是b $ b。我们要求所有这些情况都调用一个名为

SwallowException的方法。目前它只是将异常消息输出到

跟踪缓冲区。我已经在我们的应用程序中确定了许多区域,其中包含了从未应该生成的异常




服务器端应用程序更需要记录异常然后客户端

代码,因为他们无权访问UI来显示故障信息。

应用程序总是可以制定系统范围的策略决策关于记录,

未处理的例外政策等。


组件(图书馆)做出更困难的决定,因为它们是

不拥有的小块记录策略或机制。他们可能没有
甚至具有足以登录文件或创建

事件日志源的安全权限。如果您不知道环境中将使用某个组件,那么您无法对运行时可用的内容做出假设。在

这种情况​​下,您应该只抛出异常并让代码使用

组件来决定如何处理异常。


目前没有任何系统范围的通知,只要

抛出任何异常 - 这是不幸的,因为这将是有用的

,以确保异常是总是记录下来。


" Einar H?st" < _E ******* @ hotmail.com>在消息中写道

news:uw ************** @ tk2msftngp13.phx.gbl ...
As someone else wrote, start with the MS Exception Management block. You may
need to modify/extend it but it''s a pretty good starting point.

I use a strategy that boils down to this...

If it''s an exception that my own code throws then if it''s the 1st time that
an exception has been thrown I log it. If it''s being propagated up a call
stack (this happens when a catch block catches it, wraps it in another
exception, and rethrows it), I do not log it. I detect this in the
constructor of the exception object by examining the InnerException property
value.

If the exception is thrown by an external module then the catching code must
manually call the exception publish method if it is warranted.

If the exception is crossing a boundary I log it (perhaps again) - this
ensures that exception data is never lost in case the original site that
generated the exception did not log it. You must define what the boundary
is - it can be an appdomain, machine, server, module, etc.

Another thing to look for are cases where an exception gets swallowed. While
this is considered to be "bad" it nonetheless still happens, sometimes
legitimately. We require all such cases to call a method called
SwallowException. Currently it just outputs the exception message to the
trace buffer. I''ve identified a number of areas in our app where exceptions
were being swallowed that never should have been generated at all.

Server-side applications has more need to log exceptions then client-side
code since they do not have access to a UI to display fault information.
Applications can always make system-wide policy decisions about logging,
unhandled exception policy, etc.

Components (libraries) have a more difficult decision to make since they are
small pieces that do not "own" a logging policy or mechanism. They may not
even have security privileges sufficient to log to a file or create an
eventlog source. If you don''t know the environment a component will be used
in you cannot make assumptions about what will be available at runtime. In
this case you should only throw the exception and let the code using the
component make the decision about how to handle the exception.

There currently is no system-wide notification that is generated whenever
any exception is thrown - this is unfortunate because this would be useful
for ensuring that exceptions are always logged.

"Einar H?st" <_e*******@hotmail.com> wrote in message
news:uw**************@tk2msftngp13.phx.gbl...

我正在使用.NET中的Trace功能,使用它来提供我们正在开发的项目中dll所需的一些日志记录。然而,作为一个新手,我想知道一些更有经验的伐木工人是否可以提供一些关于如何以简单而灵活的方式登录的想法。对于
实例,我希望代码尽可能通过Trace
语句整洁。

作为基本日志记录功能的一个例子,我已经上来了用于记录对象中发生的异常的一些替代方法:1)在方法中抛出异常,该方法可以在抛出实际异常之前处理任何记录。

2)处理我自己的异常层次结构的构造函数。
这使得日志在代码中更加透明,但不适用于
标准。 NET例外如ArgumentNullException(我必须要
子类型)。

3)使用装饰器对象包装原始对象,捕获所有
异常,写入记录和重新抛出。

非常感谢任何和所有的想法/评论!

问候,
Einar

-
如果是这样,那可能是;如果是这样,那就是;但因为它不是b $ b,所以它不是。这是'逻辑' - 刘易斯卡罗尔
Hi,

I''m getting into the Trace-functionality in .NET, using it to provide some
much-needed logging across dlls in the project we''re working on. However,
being a newbie, I''m wondering if some more experienced loggers can provide
me with some ideas as to how to log in a simple yet flexible manner. For
instance, I''d like the code to be as uncluttered as possible by Trace
statements.

As an example of basic logging functionality, I''ve come up with some
alternatives for logging exceptions that occur in an object:

1) Wrap the throwing of an exception in a method, which can handle any
logging before throwing the actual exception.

2) Handle logging in the constructors of my own hierarchy of exceptions.
This makes the logging more transparent in the code, but doesn''t work for
standard .NET exceptions such as ArgumentNullException (I''d have to subtype it).

3) Use a decorator object to wrap the original object, catch all exceptions, write to log, and rethrow.

Any and all thoughts/comments are much appreciated!

Regards,
Einar

--
"If it was so, it might be; and if it were so, it would be; but as it isn''t, it ain''t. That''s logic" -- Lewis Carroll



这篇关于记录(例外等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆