EntLib5:Loggin应用程序块未记录到事件日志(例外:无法构造类型LogWriter) - 任何人都可以帮忙吗?

Mar*_*tin 6 .net logging enterprise-library application-blocks logging-application-block

嗨,您好,

我只是设置了Enterprise Library 5的日志记录应用程序块部分.我认为我已经完成了它,但它没有记录到事件日志,它在write方法上出错并给我以下异常.

    The type LogWriter cannot be constructed. 
         You must configure the container to supply this value.
Run Code Online (Sandbox Code Playgroud)

任何人都可以给我一张支票或告诉我我错过了什么..

首先,我使用winforms应用程序在Windows 7中运行.

这是我创建日志的方法,您可以看到write方法.

public class Logger : ILogger
{
    public void SendTest(string test)
    {
        LogEntry log = new LogEntry();
        log.EventId = 300;
        log.Message = test;
        log.Categories.Add("testing");
        log.Severity = TraceEventType.Information;
        log.Priority = 5;
        Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(log);

    }
}
Run Code Online (Sandbox Code Playgroud)

我的所有配置都是使用entlib5编辑器而不是手工编写的.并确认这是entlib.config所在的路径

    filePath="C:\myapp\entlib.config" />
Run Code Online (Sandbox Code Playgroud)

这是我的app.config,它指向我的entlib.config

<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
    <sources>
        <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            filePath="C:\myapp\entlib.config" />
    </sources>
    <redirectSections>
        <add sourceName="File-based Configuration Source" name="Redirected Section" />
    </redirectSections>
</enterpriseLibrary.ConfigurationSource>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的entlib.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>
Run Code Online (Sandbox Code Playgroud)

编辑 问题已使用新的app.config进行更新,以包含

<redirectSections>
    <add sourceName="File-based Configuration Source" name="Redirected Section" />
</redirectSections>
Run Code Online (Sandbox Code Playgroud)

我仍然收到错误.

 Activation error occured while trying to get instance of type
Run Code Online (Sandbox Code Playgroud)

LogWriter,键""

内在的例外是

解析依赖项失败,类型= \"Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter \",name = \"(none)\".\ r \n发生异常时:解析时.\ r \nException是:InvalidOperationException - 无法构造LogWriter类型.您必须配置容器以提供此值.\ r \n ------------------------------------ -----------\r \n在异常时,容器是:\ r \n\r \n解析Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(无)\ r\N"}

我使用EntLib配置器来创建这两个文件.

entlib.config我接受了默认值,因为它似乎包含了EventLog监听器.

编辑

这是我在entlib.config文件中的行,log是空的 - 这是正确的吗?

    <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="None" />
Run Code Online (Sandbox Code Playgroud)

编辑

我的Logger.write是一个DLL,但我的app.config/entlib.config是在一个winforms应用程序.. winforms应用程序有一个dll项目的引用,我写日志.

dll和winforms都有引用

   Microsoft.Practices.EnterpriseLibrary.Logging
Run Code Online (Sandbox Code Playgroud)

并且还使用了统一性

   Microsoft.Practices.Unity

   Microsoft.Practices.Unity.Interception
Run Code Online (Sandbox Code Playgroud)

Gri*_*nik 3

Martin,您已经创建了一个基于文件的配置源,但尚未将日志记录部分重定向到它。

将以下内容添加到 enterpriseLibrary.ConfigurationSource 节点内的 App.config 中:

<redirectSections>
      <add sourceName="File-based Configuration Source" name="loggingConfiguration" />
</redirectSections>
Run Code Online (Sandbox Code Playgroud)

此外,我推荐企业库 5.0 实践实验室集中的配置源实验室。它涵盖了这种情况以及更高级的场景。