gpt4 book ai didi

c# - 尝试获取 LogWriter 类型的实例时发生激活错误, key ""?

转载 作者:可可西里 更新时间:2023-11-01 08:37:21 31 4
gpt4 key购买 nike

我有 asp.net 网站。我在站点中添加了 Microsoft.Practices.EnterpriseLibrary.Logging.dll 的引用。

在 web.cofig 文件中,我定义如下。

    <?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="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
fileName="C:\Temp\RollingFlatFile.log" footer="----------------------------------------"
formatter="Text Formatter" header="----------------------------------------"
rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20"
timeStampPattern="yyyy-MM-dd" maxArchivedFiles="3" traceOutputOptions="None"
filter="All" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp(local)}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}ActivityId: {property(ActivityId)}{tab}Severity: {severity}{tab}Title:{title}{tab}"
name="Brief Format Text" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}Severity: {severity}{tab}Title: {title}{tab}Activity ID: {property(ActivityId)}{tab}Machine: {localMachine}{tab}App Domain: {localAppDomain}{tab}ProcessId: {localProcessId}{tab}Process Name: {localProcessName}{tab}Thread Name: {threadName}{tab}Win32 ThreadId:{win32ThreadId}{tab}Extended Properties: {dictionary({key} - {value}{tab})}"
name="Text Formatter" />
</formatters>
<logFilters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
minimumPriority="2" maximumPriority="99" name="Priority Filter" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
enabled="true" name="LogEnabled Filter" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
<categoryFilters>
<add name="BlockedByFilter" />
</categoryFilters>
</add>
</logFilters>
<categorySources>
<add switchValue="All" name="Important">
<listeners>
<add name="Formatted EventLog TraceListener" />
<add name="Rolling Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="UnprocessedFlatFile" />
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>

当我运行代码异常发生在下面的地方。

defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
//Activation error occured while trying to get instance of type LogWriter, key ""

如何解决?

最佳答案

您的完整配置引用了未定义的跟踪监听器。这会导致企业库运行时在尝试解析这些引用时抛出运行时异常。

我注释掉了未使用的监听器(并将默认类别更改为 Important)并且它正在运行:

<?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="Logging Application Block" tracingEnabled="true"
defaultCategory="Important" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
fileName="C:\Temp\RollingFlatFile.log" footer="----------------------------------------"
formatter="Text Formatter" header="----------------------------------------"
rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20"
timeStampPattern="yyyy-MM-dd" maxArchivedFiles="3" traceOutputOptions="None"
filter="All" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp(local)}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}ActivityId: {property(ActivityId)}{tab}Severity: {severity}{tab}Title:{title}{tab}"
name="Brief Format Text" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp}{tab}Message: {message}{tab}Category: {category}{tab}Priority: {priority}{tab}EventId: {eventid}{tab}Severity: {severity}{tab}Title: {title}{tab}Activity ID: {property(ActivityId)}{tab}Machine: {localMachine}{tab}App Domain: {localAppDomain}{tab}ProcessId: {localProcessId}{tab}Process Name: {localProcessName}{tab}Thread Name: {threadName}{tab}Win32 ThreadId:{win32ThreadId}{tab}Extended Properties: {dictionary({key} - {value}{tab})}"
name="Text Formatter" />
</formatters>
<logFilters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
minimumPriority="2" maximumPriority="99" name="Priority Filter" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
enabled="true" name="LogEnabled Filter" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
<categoryFilters>
<add name="BlockedByFilter" />
</categoryFilters>
</add>
</logFilters>
<categorySources>
<add switchValue="All" name="Important">
<listeners>
<!--<add name="Formatted EventLog TraceListener" />-->
<add name="Rolling Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<!--<add name="UnprocessedFlatFile" />-->
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<!--<add name="Formatted EventLog TraceListener" />-->
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>

线索在内部异常中。在 InnerException.Message 的末尾,它说:

Resolving System.Diagnostics.TraceListener,Formatted EventLog TraceListener

以上配置在app.config中。然后 Program.cs 包含:

class Program
{
static void Main(string[] args)
{
LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
logWriter.Write("Test");
}
}

关于c# - 尝试获取 LogWriter 类型的实例时发生激活错误, key ""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7813925/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com