EntLib5:Loggin应用程序块未记录到事件日志中(例外:无法构造LogWriter类型)-有人可以帮忙吗? [英] EntLib5: Loggin application block not logging to Event Log (exception: type LogWriter cannot be constructed)- Can anyone help?

查看:99
本文介绍了EntLib5:Loggin应用程序块未记录到事件日志中(例外:无法构造LogWriter类型)-有人可以帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是设置了Enterprise Library 5的日志记录应用程序块部分.我想我已经做对了,但是没有将其记录到事件日志中,它在写入方法上出错,并且给了我以下异常.

I just setup the logging application block part of Enterprise Library 5. I think i have done it correct but its not logging to the event log it errors on the write method and gives me the following exception.

    The type LogWriter cannot be constructed. 
         You must configure the container to supply this value.

任何人都可以给我支票或告诉我我所缺少的..

Can anyone do me a check or tell me what i am missing..

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

First of all i am running in windows 7 with a winforms application.

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

Here is my method that creates the log and you see the write method.

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);

    }
}

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

All my configs were built using the entlib5 editor and not by hand. And confirmed this si the path where entlib.config is

    filePath="C:\myapp\entlib.config" />

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

Here is my app.config which POINTS to my 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>

这是我的entlib.config

and here is my 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>

编辑 该问题已使用新的app.config更新,包括了

EDIT The question has been updated with the new app.config, to include

<redirectSections>
    <add sourceName="File-based Configuration Source" name="Redirected Section" />
</redirectSections>

我仍然遇到错误.

 Activation error occured while trying to get instance of type

LogWriter,键为"

LogWriter, key ""

内部异常是

解决依赖关系失败, 类型= \"Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter \", name = \(无)\".\ r \ nException 发生时间: 解决.\ r \ nException为: InvalidOperationException-类型 无法构造LogWriter.你 必须配置要供应的容器 这 值.\ r \ n -------------------------------------------- --- \ r \ n发生例外时, 容器为:\ r \ n \ r \ n正在解析 Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(无)\ r \ n}

Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(none)\r\n"}

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

I used the EntLib configurator to create both files.

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

The entlib.config i accepted the default as it appears to have the EventLog listener included..

编辑

这是我在entlib.config文件中的行,日志为空-ermm对吗?

this is my line in the entlib.config file, log is empty-- ermm is this correct?

    <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" />

编辑

我的Logger.write在dll中,但是我的app.config/entlib.config在winforms应用中..winforms应用引用了我在其中写入日志的dll项目.

MY Logger.write is in a dll but my app.config / entlib.config are in a winforms app.. the winforms app has a reference to the dll project where i write the log.

dll和winforms都有对

Both the dll and the winforms has a reference to

   Microsoft.Practices.EnterpriseLibrary.Logging

并且也使用统一性,具有

and also using unity, has

   Microsoft.Practices.Unity

   Microsoft.Practices.Unity.Interception

推荐答案

马丁,您已经创建了基于文件的配置源,但是尚未将日志记录部分重定向到该文件.

Martin, you've created a file-based configuration source, but you haven't redirected the logging section to it.

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

Add the following to App.config inside enterpriseLibrary.ConfigurationSource node:

<redirectSections>
      <add sourceName="File-based Configuration Source" name="loggingConfiguration" />
</redirectSections>

此外,我建议从

In addition, I recommend the Configuration Sources Labs from the Enterprise Library 5.0 Hands-on Labs set. It covers this and more advanced scenarios.

这篇关于EntLib5:Loggin应用程序块未记录到事件日志中(例外:无法构造LogWriter类型)-有人可以帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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