从 C# 程序的命令行使用 App.Config
我正在学习 log4net,目前正在测试如何使用 App.Config 进行 XMLConfiguration。
问题是我办公室没有 .NET IDE,例如 Visual Studio 2008 或 Express Edition(不要让我开始了解为什么/如何:-))
我需要编译并运行我的代码,其中 log4net 读取配置App.Config 中的设置。我该怎么做?
我的 C# 代码如下。
public class BasicXMLConfiguration
{
public static void Main (string [] args)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log =
log4net.LogManager.GetLogger(typeof(BasicXMLConfiguration));
log.Info("beginning of loop");
for (int c = 0; c < 10; c++)
{
log.DebugFormat("Loop Count is {0}", c);
}
log.Info("looping ends");
}
}
我的 App.Config 如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!--
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>
-->
<!-- Never, ever use the FileAppender. Instead, use the RollingFileAppender -->
<!--
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
<appendToFile value="true" />
<encoding value="utf-8"/>
<layout type="log4net.Layout.SimpleLayout" />
</appender>
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
我使用的命令是这样
csc BasicXMLConfiguration.cs /r:log4net.dll
它编译得很好。但是,当我运行 exe 时,
BasicXMLConfiguration.exe
出现以下错误。
log4net:ERROR XmlConfigurator: Failed
to find configuration section 'log4net' in the application's
.config file. Check your .config file for the <log4net> and <
configSections> elements. The configuration section should look
like: <section n ame="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
如何让它引用App.Config?
I am learning log4net and currently testing out how to use App.Config for XMLConfiguration.
The problem is that I do not have a .NET IDE such as Visual Studio 2008 or Express Edition at office (don't get me started on why/how :-))
I need to compile and run my code where log4net reads the configuration settings from App.Config. How do I do it?
My C# code is as follows.
public class BasicXMLConfiguration
{
public static void Main (string [] args)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log =
log4net.LogManager.GetLogger(typeof(BasicXMLConfiguration));
log.Info("beginning of loop");
for (int c = 0; c < 10; c++)
{
log.DebugFormat("Loop Count is {0}", c);
}
log.Info("looping ends");
}
}
My App.Config is as follows
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!--
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>
-->
<!-- Never, ever use the FileAppender. Instead, use the RollingFileAppender -->
<!--
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
<appendToFile value="true" />
<encoding value="utf-8"/>
<layout type="log4net.Layout.SimpleLayout" />
</appender>
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
My command that I use is this
csc BasicXMLConfiguration.cs /r:log4net.dll
It compiles fine. However, when I run the exe as
BasicXMLConfiguration.exe
I get the following error.
log4net:ERROR XmlConfigurator: Failed
to find configuration section 'log4net' in the application's
.config file. Check your .config file for the <log4net> and <
configSections> elements. The configuration section should look
like: <section n ame="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
How do I make it to reference the App.Config?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
app.config
重命名为name_of_your_exe.config
。假设您的控制台应用程序名称是 Log4NetTest.exe,然后将app.config
重命名为Log4Net.exe.config
就可以了。作为名称您的程序是
BasicXMLConfiguration.exe
,因此将app.config
重命名为BasicXMLConfiguration.exe.config
,它将起作用。you need to rename
app.config
toname_of_your_exe.config
.Suppose your console application name is Log4NetTest.exe then renameapp.config
toLog4Net.exe.config
and it will be fine.As name of your program is
BasicXMLConfiguration.exe
so renameapp.config
toBasicXMLConfiguration.exe.config
and it will work.