统一:类型日志写不能被建造 [英] Unity: The type LogWriter cannot be constructed

查看:141
本文介绍了统一:类型日志写不能被建造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关下面的工程我得到解决统一为DI时,一个非常恼人的和令人费解的错误




出现InvalidOperationException - 类型日志写无法构造。
必须配置为提供此值的容器



ex.Message?;在解析:依赖解析失败,键入=
\WindowsFormsApplication1.Performance\,名称=
\(无)\.\r\\\
Exception发生时.\r\\\
Exception
为:InvalidOperationException异常 - 类型日志写不能建造
必须配置提供这种
value.\r\\\
容器----。 ------------------------------------------- \r\\\
At的例外的
时,该容器是:\r\\\
\r\\\
解决
WindowsFormsApplication1.Performance,(无)\r\\\
解析参数
\\ \\构造
WindowsFormsApplication1.Performance(Microsoft.P​​ractices.EnterpriseLibrary.Logging.LogWriter
LW,
Microsoft.P​​ractices.EnterpriseLibrary.ExceptionHandling.ExceptionManager
EM)的lw\ \r\\\
解决
Microsoft.P​​ractices.EnterpriseLibrary.Logging.LogWriter,(无)\r\\\



?ex.StackTrace ; 在
Microsoft.P​​ractices.Unity.UnityContainer.DoBuildUp(T型,对象
现有的字符串名称,IEnumerable的 1 resolverOverrides)在$ B $是:\\Builds \\Unity\\UnityTemp\\Compile\\Unity\\Unity\\Src\\UnityContainer.cs:行
515\r\\\
在Microsoft.P​​ractices.Unity.UnityContainer.DoBuildUp(输入
T,字符串名称,IEnumerable的
1 resolverOverrides)在$ b $是:\Builds\Unity\UnityTemp\Compile\\ \\Unity\Unity\Src\UnityContainer.cs:在Microsoft.P​​ractices.Unity.UnityContainer.Resolve(T型,
字符串名称,ResolverOverride [在线
485\r\\\
] resolverOverrides)在$ b $是:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:在$ b线
173\r\\\
$ b Microsoft.P​​ractices.Unity.UnityContainerExtensions.Resolve [T](IUnityContainer
容器,ResolverOverride []驰)在$ b $是:\Builds\Unity\UnityTemp\Compile\Unity\ Unity\Src\UnityContainerExtensions.cs:在WindowsFormsApplication1.Form1.OnLoad(EventArgs五)在
D线
504\r\\\
:\Devzone\Tasking\WindowsFormsApplication1\ Form1.cs中:第33行




在一个表格:

 保护覆盖无效的OnLoad(EventArgs的发送)
{
base.OnLoad(E);


{
IUnityContainer集装箱=新UnityContainer();
演奏P = container.Resolve<性能与GT;();
}
赶上(异常前)
{

}
}

相关性类:

 公共类性能
{
酒店的公共性能(日志写LW,ExceptionManager EM)
{
}
}

配置文件:<?/ p>

 < XML版本=1.0编码=UTF-8> 
<结构>
< configSections>
<节名称=loggingConfigurationTYPE =Microsoft.P​​ractices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,Microsoft.P​​ractices.EnterpriseLibrary.Logging,版本= 5.0.414.0,文化=中性公钥= 31bf3856ad364e35requirePermission =真/>
<节名称=exceptionHandlingTYPE =Microsoft.P​​ractices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,Microsoft.P​​ractices.EnterpriseLibrary.ExceptionHandling,版本= 5.0.414.0,文化=中性公钥= 31bf3856ad364e35requirePermission =真/>
< / configSections>
< loggingConfiguration NAME =tracingEnabled =真defaultCategory =常规>​​;
<&听众GT;
<添加名称=事件日志监听器类型=Microsoft.P​​ractices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,Microsoft.P​​ractices.EnterpriseLibrary.Logging,版本= 5.0.414.0,文化=中性公钥= 31bf3856ad364e35
listenerDataType =Micr​​osoft.P​​ractices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,Microsoft.P​​ractices.EnterpriseLibrary.Logging,版本= 5.0.414.0,文化=中性公钥= 31bf3856ad364e35
源=企业库记录格式化=文本格式化
日志=MACHINENAME =。 traceOutputOptions =无/>
< /听众>
<格式化>
<加上TYPE =Microsoft.P​​ractices.EnterpriseLibrary.Logging.Formatters.TextFormatter,Microsoft.P​​ractices.EnterpriseLibrary.Logging,版本= 5.0.414.0,文化=中性公钥= 31bf3856ad364e35
模板= 时间戳:{时间戳} {换行符}&放大器; #xA;消息:{消息} {换行符}&放大器; #xA;类别:{类别} {换行符}&放大器; #xA;优先级:{优先权} {换行符}&安培; #xA;事件ID:{事件ID} {}换行和放大器; #xA;严重性:{严重} {}换行和放大器; #xA;标题是:{title} {}换行和放大器; #xA;机:{LOCALMACHINE} {}换行符&安培; #xA;应用程序域:{localAppDomain} {换行符}&放大器; #xA;的ProcessID:{localProcessId} {换行符}&放大器; #xA;进程名称:{localProcessName} {换行符}&放大器; #xA;螺纹名称:{ threadName} {换行符}&放大器; #xA;的Win32的ThreadId:{win32ThreadId} {换行符}&放大器; #xA;扩展属性:{字典({键} - {值} {换行符})}
NAME =文本格式化/>
< /格式化>
< categorySources>
<添加switchValue =全部NAME =常规>​​;
<&听众GT;
<添加名称=事件日志监听器/>
< /听众>
< /添加>
<添加switchValue =全部NAME =组别/>
< / categorySources>
< specialSources>
< allEvents switchValue =全部NAME =所有事件/>
< notProcessed switchValue =全部NAME =未处理类别/>
<错误switchValue =全部NAME =记录错误和放大器;放大器;警告>
<&听众GT;
<添加名称=事件日志监听器/>
< /听众>
< /错误>
< / specialSources>
< / loggingConfiguration>
< exceptionHandling>
< exceptionPolicies>
<添加名称=政策>
< exceptionTypes>
<添加名称=所有异常TYPE =System.Exception的,mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089
postHandlingAction =NotifyRethrow/>
< / exceptionTypes>
< /添加>
< / exceptionPolicies>
< / exceptionHandling>
< /结构>


解决方案

您需要的企业库扩展添加到您的容器。没有它,容器不读取配置文件,因此不知道如何创建Entlib对象,像日志写进程。


For the following "project" I am getting a very annoying and inexplicable error when resolving Unity for DI.

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

?ex.Message; "Resolution of the dependency failed, type = \"WindowsFormsApplication1.Performance\", 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 WindowsFormsApplication1.Performance,(none)\r\n Resolving parameter \"lw\" of constructor WindowsFormsApplication1.Performance(Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter lw, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager em)\r\n Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(none)\r\n"

?ex.StackTrace; " at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable1 resolverOverrides) in e:\\Builds\\Unity\\UnityTemp\\Compile\\Unity\\Unity\\Src\\UnityContainer.cs:line 515\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 485\r\n at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 173\r\n at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainerExtensions.cs:line 504\r\n at WindowsFormsApplication1.Form1.OnLoad(EventArgs e) in D:\Devzone\Tasking\WindowsFormsApplication1\Form1.cs:line 33"

In a form:

  protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                IUnityContainer container = new UnityContainer();
                Performance p = container.Resolve<Performance>();
            }
            catch (Exception ex)
            {

            }
        }

Dependency class:

 public class Performance
    {
        public Performance(LogWriter lw, ExceptionManager em)
        {
        }
    }

Configuration File:

<?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" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, 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>
            <add switchValue="All" name="Category2" />
        </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>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Policy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow" />
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

解决方案

You need to add the Enterprise Library extension to your container. Without it, the container doesn't read the config file and therefore doesn't know how to create Entlib objects, like the LogWriter.

这篇关于统一:类型日志写不能被建造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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