.net core 配置Enterprise Library
时间: 2023-11-22 10:53:51 浏览: 157
要在 .NET Core 中使用 Enterprise Library,需要执行以下步骤:
1. 安装 NuGet 包:在项目中安装 Enterprise Library 的 NuGet 包。在 Visual Studio 中,右键单击项目并选择“Manage NuGet Packages”,然后搜索“EnterpriseLibrary”并安装。
2. 添加配置文件:在项目中添加一个名为“app.config”的配置文件,并将以下代码添加到文件中:
```
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource"
type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<sources>
<add name="File Configuration Source"
type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath=".\App.config" />
</sources>
<redirectSections>
<section name="loggingConfiguration"
sourceName="File Configuration Source" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
<loggingConfiguration name="" tracingEnabled="true"
defaultCategory="General">
<listeners>
<add name="Rolling Flat File Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\logs\myapp.log"
footer="--------------------------"
formatter="Text Formatter"
header="--------------------------"
rollFileExistsBehavior="Increment"
rollInterval="Day"
rollSizeKB="1024"
timeStampPattern="yyyy-MM-dd" />
</listeners>
<formatters>
<add name="Text Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="{timestamp} [{category}] {message}" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
</loggingConfiguration>
</configuration>
```
这个配置文件包含一个名为“Rolling Flat File Trace Listener”的日志记录器,它将日志记录到文件中。你可以根据自己的需要更改此配置。
3. 创建 Enterprise Library 实例:在代码中创建 Enterprise Library 实例,并使用它来记录日志。以下是一个示例:
```
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
ILogger logger = EnterpriseLibraryContainer.Current.GetInstance<ILogger>();
logger.Write("Hello, world!");
```
这将使用配置文件中指定的日志记录器记录一条消息。
希望这可以帮助你配置 Enterprise Library 在 .NET Core 中运行。
阅读全文