我正在尝试使用Common Infrastructure Libraries for .NET(Common.Logging)和Enterprise Library 4.1的日志记录应用程序块。根据docs,这是支持的。如何配置Microsoft企业库日志记录应用程序块以处理任何日志记录类别?
我遇到的问题是,当我尝试登录的东西,像这样(在这个例子中,我在ASP.NET MVC应用程序):
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
ILog log = LogManager.GetCurrentClassLogger();
log.Info("Hello world!");
return View();
}
而不只是简单的日志消息,我在事件日志中收到错误:
消息:没有类别'TestApp.Controllers.HomeController'的显式映射。
那么,Common.Logging中似乎没有任何选项来设置默认类别,并且我无法弄清楚如何配置LoggingConfiguration以接受任何类别,即使它不是定义。
这里是我的LoggingConfiguration的一个片段:
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
...
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Formatted EventLog TraceListener" />
<add name="Email TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
我试过设置logWarningsWhenNoCategoriesMatch
为假,但只是沉默的警告 - 它没有记录工作。
我也试过摆脱<notProcessed switchValue="All" .../>
特殊来源,但这似乎也没有任何效果。
任何人都知道是否有办法让这个工作?谢谢!
@Tuxo - 真棒!我没有意识到'GetLogger'会以这种方式工作 - 我的__assumption__(d'oh)是'GetLogger'会返回一个匹配给定类型的记录器,有点像ServiceLocator模式。现在命名对我来说更有意义。谢谢!!! – Pandincus 2011-03-21 17:19:32
@Pandincus - 有一个GetLogger负载需要一个Type,这可能是您想要的。 – 2011-03-21 17:49:04