65.9K
CodeProject 正在变化。 阅读更多。
Home

开发日志示波器 - 第一部分:开发概念

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.26/5 (14投票s)

2007年1月2日

CPOL

1分钟阅读

viewsIcon

44313

介绍概念

引言

在大多数企业级应用中,日志记录与任何其他业务需求一样重要。例如,一个网上银行站点如果没有日志就无法运行。

什么是日志记录?

在我看来,应用程序日志是模型上所有事件和操作的记录,其中包含提供审计跟踪所需的信息。

记录的信息应该能够用于回滚和/或重放所有或选定的操作。

一个很好的例子是 SQL Server 的事务日志。

什么是跟踪?

另一方面,跟踪是应用程序本身中事件的记录。

记录的信息可用于跟踪应用程序行为,并且绝不应为业务所必需。

仍然以 SQL Server 为例,跟踪就是 SQL Server 记录在 Windows EventLog 中的内容。

什么是日志范围?

日志范围是需要作为单元记录的事件和操作的范围。

日志范围可以是网页到 Web 服务器的请求/响应,可以是调用 Web 服务,可以是检索数据库中的一些信息等等。

日志范围的先决条件是什么?

  1. 如果存在,则可以加入当前的日志范围
  2. 可以随时启动新的日志范围
  3. 应用程序层面的独立性
  4. 环境的独立性
  5. 订阅者的独立性
  6. 拥有头部
  7. 拥有多个细节

使用模式

最佳使用模式将是一个实现 IDisposable 的代理类,这将允许使用 using (C#, VB) 块,从而提高代码的可读性和可维护性。

[C#]
using (LogScope log = new LogScope())
{
  ...
  log.Publish(message);
  ...
}
[VB.NET]
Using log as New LogScope()
  ...
  log.Publish(message)
  ...
End Using

创建日志范围代理

日志范围代理实例的创建应通过实例化带有代理设置参数的类实例来进行。

[C#]
/// <summary>
/// Initializes a new log scope proxy that joins the current log scope, if one exists.
/// </summary>
public LogScope();

/// <summary>
/// Initializes a new log scope proxy that joins a specified log scope, if one exists.
/// </summary>
/// <param name="logScope">The log scope to join.<//param>
public LogScope(LogScope logScope);

/// <summary>
/// Initializes a new log scope proxy that starts a new log scope 
/// from the current log scope.
/// </summary>
/// <param name="logHeader">The log header.<//param>
public LogScope(object logHeader);

/// <summary>
/// Initializes a new log scope proxy that starts a new log scope 
/// from the specified log scope.
/// </summary>
/// <param name="logScope">The log scope.<//param>
/// <param name="logHeader">The log header.<//param>
public LogScope(LogScope logScope, object logHeader);

/// <summary>
/// Initializes a new log scope proxy from the current log scope.
/// </summary>
/// <param name="logMaster">The log master.<//param>
/// <param name="requiresNew">If set to <see langword="true" />, 
/// starts a new log scope.<//param>
public LogScope(object logMaster, bool requiresNew);

/// <summary>
/// Initializes a new log scope proxy from the specified log scope.
/// </summary>
/// <param name="logScope">The log scope.<//param>
/// <param name="logHeader">The log header.<//param>
/// <param name="requiresNew">If set to <see langword="true" />, 
/// starts a new log scope.<//param>
public LogScope(LogScope logScope, object logHeader, bool requiresNew);

[VB.NET]
''' <summary>
''' Initializes a new log scope proxy that joins the current log scope, if one exists.
''' </summary>
Public Sub New()

''' <summary>
''' Initializes a new log scope proxy that joins a specified log scope, if one exists.
''' </summary>
''' <param name="logScope">The log scope to join.<//param>
Public Sub New(ByVal logScope As LogScope)

''' <summary>
''' Initializes a new log scope proxy that starts a new log scope 
''' from the current log scope.
''' </summary>
''' <param name="logHeader">The log header.<//param>
Public Sub New(ByVal logHeader As Object)

''' <summary>
''' Initializes a new log scope proxy that starts a new log scope 
''' from the specified log scope.
''' </summary>
''' <param name="logScope">The log scope.<//param>
''' <param name="logHeader">The log header.<//param>
Public Sub New(ByVal logScope As LogScope, ByVal logHeader As Object)

''' <summary>
''' Initializes a new log scope proxy from the current log scope.
''' </summary>
''' <param name="logMaster">The log master.<//param>
''' <param name="requiresNew">If set to <SEE langword="true" />, 
''' starts a new log scope.<//param>
Public Sub New(ByVal logMaster As Object, ByVal requiresNew As Boolean)

''' <summary>
''' Initializes a new log scope proxy from the specified log scope.
''' </summary>
''' <param name="logScope">The log scope.<//param>
''' <param name="logHeader">The log header.<//param>
''' <param name="requiresNew">If set to <SEE langword="true" />, 
''' starts a new log scope.<//param>
Public Sub New(ByVal logScope As LogScope, _
	ByVal logHeader As Object, ByVal requiresNew As Boolean)
© . All rights reserved.