在一个解决方案中用于项目和单元测试的 log4net 配置
log4net configuration for project and unit test in one solution
我的解决方案包含一个 Windows 服务应用程序项目和一个单元测试项目,如下所示:
--------------------Windows 服务应用程序项目设置---------------- ---
我在我的Windows服务应用程序项目中设置了log4net,如下所示:
步骤 1) 我已将对 log4net 的引用添加到我的 Windows 服务应用程序项目中。
步骤 2) 我的 app.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message %exception%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
步骤 3) 我已将此添加到 Program.cs 主要部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyAppService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
步骤 4) 在我登录的 classes 中,我添加了这个:
private static ILog logger = LogManager.GetLogger(typeof(Reader));
步骤 5) 我添加了这样的日志记录语句:
logger.Info("Data Read Completed Successfully.");
----------------------------单元测试项目设置------------ --------------
我在我的单元测试项目中设置了 log4net,如下所示:
步骤 1) 我已将对 log4net 的引用添加到我的单元测试项目中。
步骤 2) 我添加了一个 app.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
</configuration>
步骤 3) 我已将此添加到 ForecastIOTest.cs 单元测试 class:
using log4net;
using log4net.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using WeatherAppService;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace WeatherAppTests
{
[TestClass]
public class ForecastIOTest
{
private static ILog logger = LogManager.GetLogger(typeof(WeatherController));
[AssemblyInitialize]
public static void Configure(TestContext tc)
{
log4net.Config.XmlConfigurator.Configure();
}
步骤 4) 在我记录日志的 class 测试中,我添加了这个:
private static ILog logger = LogManager.GetLogger(typeof(ForecastIOTest));
步骤 5) 我添加了这样的日志记录语句:
logger.Info("This is a test.");
------------------------------------问题------ ----------------------------
现在,我收到的错误表明我有多个 configSections 元素:一个在服务应用程序中,另一个在测试应用程序中。
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 9)
但是如果我从测试应用程序中删除 configSections,我会收到此错误:
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 7)
------------------------------------问题-------- ----------------------------
有人知道如何设置 log4net,所以我可以 运行 单元测试,运行 服务应用程序并将我的日志文件写入其中?或者,鉴于我所做的一切,有人可以告诉我什么是不正确或不完整的吗?
错误是因为,configSection 应该是配置文件中<Configuration>
之后的第一个元素。
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<ConfigSections>
元素指定配置和处理程序声明,因此这必须是 <Configuration>
的第一个子元素
我的解决方案包含一个 Windows 服务应用程序项目和一个单元测试项目,如下所示:
--------------------Windows 服务应用程序项目设置---------------- ---
我在我的Windows服务应用程序项目中设置了log4net,如下所示:
步骤 1) 我已将对 log4net 的引用添加到我的 Windows 服务应用程序项目中。
步骤 2) 我的 app.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message %exception%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
步骤 3) 我已将此添加到 Program.cs 主要部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyAppService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
步骤 4) 在我登录的 classes 中,我添加了这个:
private static ILog logger = LogManager.GetLogger(typeof(Reader));
步骤 5) 我添加了这样的日志记录语句:
logger.Info("Data Read Completed Successfully.");
----------------------------单元测试项目设置------------ --------------
我在我的单元测试项目中设置了 log4net,如下所示:
步骤 1) 我已将对 log4net 的引用添加到我的单元测试项目中。
步骤 2) 我添加了一个 app.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
</configuration>
步骤 3) 我已将此添加到 ForecastIOTest.cs 单元测试 class:
using log4net;
using log4net.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using WeatherAppService;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace WeatherAppTests
{
[TestClass]
public class ForecastIOTest
{
private static ILog logger = LogManager.GetLogger(typeof(WeatherController));
[AssemblyInitialize]
public static void Configure(TestContext tc)
{
log4net.Config.XmlConfigurator.Configure();
}
步骤 4) 在我记录日志的 class 测试中,我添加了这个:
private static ILog logger = LogManager.GetLogger(typeof(ForecastIOTest));
步骤 5) 我添加了这样的日志记录语句:
logger.Info("This is a test.");
------------------------------------问题------ ----------------------------
现在,我收到的错误表明我有多个 configSections 元素:一个在服务应用程序中,另一个在测试应用程序中。
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 9)
但是如果我从测试应用程序中删除 configSections,我会收到此错误:
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 7)
------------------------------------问题-------- ----------------------------
有人知道如何设置 log4net,所以我可以 运行 单元测试,运行 服务应用程序并将我的日志文件写入其中?或者,鉴于我所做的一切,有人可以告诉我什么是不正确或不完整的吗?
错误是因为,configSection 应该是配置文件中<Configuration>
之后的第一个元素。
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<ConfigSections>
元素指定配置和处理程序声明,因此这必须是 <Configuration>