使用日志应用程序块构建简单的日志记录 HTTP 模块
本文介绍了一个使用日志应用程序块和 Enterprise Library 5 流式配置 API 进行日志记录的 HTTP 模块。
我想了解如何使用 Enterprise Library 5 中 日志应用程序块 的 流式配置 API。
所以我心想,为什么不使用 HTTP 模块来实现它,并在一个帖子中提供两个示例呢?
使用日志应用程序块的流式配置 API
我之前写过一篇关于 Enterprise Library 5 中 流式配置 的文章,链接地址为:流式配置。以下是我配置 Enterprise Library 容器以在简单的平面文件场景中使用 日志应用程序块 的方法。
private void ConfigureEnterpriseLibraryContainer()
{
var builder = new ConfigurationSourceBuilder();
builder.ConfigureInstrumentation().EnableLogging();
builder.ConfigureLogging().WithOptions
.LogToCategoryNamed("General")
.WithOptions
.SetAsDefaultCategory()
.SendTo
.FlatFile("Log File")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Textformatter"))
.ToFile("file.log");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current =
EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}
请注意,为了使用此 API,您需要添加对以下 DLL 的引用:
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.ServiceLocation
System.Configuration
现在我们已经将配置放置在内存中,就可以在代码中使用它了。
构建 HTTP 模块
为了构建 HTTP 模块,我们只需要实现 IHttpModule
接口。以下是一个简单的 LoggingHttpModule
,它会在 Web 请求开始和结束时记录详细信息。
using System;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace HttpModules
{
public class LoggingHttpModule : IHttpModule
{
#region Members
private LogWriter _writer;
#endregion
#region IHttpModule Members
public void Dispose()
{
if (_writer != null)
{
_writer.Dispose();
}
}
public void Init(HttpApplication context)
{
CreateLogWriter();
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
private void CreateLogWriter()
{
ConfigureEnterpriseLibraryContainer();
_writer = EnterpriseLibraryContainer.Current.
GetInstance<LogWriter>();
}
private void ConfigureEnterpriseLibraryContainer()
{
var builder = new ConfigurationSourceBuilder();
builder.ConfigureInstrumentation().EnableLogging();
builder.ConfigureLogging().WithOptions
.LogToCategoryNamed("General")
.WithOptions
.SetAsDefaultCategory()
.SendTo
.FlatFile("Log File")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Textformatter"))
.ToFile("file.log");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current =
EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}
void context_BeginRequest(object sender, EventArgs e)
{
_writer.Write(new LogEntry
{
Message = "BeginRequest"
});
}
void context_EndRequest(object sender, EventArgs e)
{
_writer.Write(new LogEntry
{
Message = "EndRequest"
});
}
#endregion
}
}
使用模块
为了使用该模块,我们只需要添加对包含 LoggingHttpModule
的类库的引用。然后我们需要在 web.config 文件中的 httpModules
元素中注册该模块,如下所示:
<httpModules>
<add name="LoggingHttpModlue"
type="HttpModules.LoggingHttpModule, HttpModules"/>
</httpModules>
就是这样。现在,每当请求开始或结束时,该模块将被执行。
摘要
使用 Enterprise Library 5 的 流式配置 API 可以轻松配置日志记录等跨切关注点,而无需配置文件。该 API 简单易用。 在本文中,我展示了如何将 日志应用程序块 与 HTTP 模块以及 流式配置 API 结合使用。