Log4Net 配置问题
所以我有一个网站和一个每天运行的控制台应用程序。
他们都调用一个名为 ProcessIncident() 的函数。该网站允许您针对单个事件手动执行此操作,并且控制台应用程序每晚执行一批操作。
在函数中,我有各种 log4net Log.InfoFormat() 和 Log.DebugFormat() 调用
当我从网站运行时,它记录正常
当我从控制台应用程序运行时,它根本不记录
指定的路径明确存在
控制台应用程序配置如下
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<logger name="NHibernate">
<level value="OFF" />
<appender-ref ref="NHibernateFileAppender" />
</logger>
<logger name="SMS">
<level value="ALL" />
<appender-ref ref="SmsFileAppender" />
</logger>
<appender name="SmsFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\XXXX\SMS.IncidentBilling.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %-5level %logger %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="NHibernateFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="D:\Dev\SMS\Main\Source\SMS.Website\Logs\nhibernate.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %appdomain %-5level %c %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
在控制台应用程序中,我引用了 log4net dll 并将 amember 声明为
private ILog Log = LogManager.GetLogger(typeof(Task));
任何人都可以看到我正在做的任何愚蠢的事情。没有实际的错误,只是通过控制台应用程序没有记录任何内容
So I've got a website and a console app that runs daily.
They both call a Function called ProcessIncident(). The website allows you to do it manually for an individual incident and the console app does a batch every night.
Within the function I have various log4net Log.InfoFormat() and Log.DebugFormat() calls
When I run from the website it logs fine
when I run from the console app it doesn't log at all
The path specified definatly exists
The console app config is as follows
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<logger name="NHibernate">
<level value="OFF" />
<appender-ref ref="NHibernateFileAppender" />
</logger>
<logger name="SMS">
<level value="ALL" />
<appender-ref ref="SmsFileAppender" />
</logger>
<appender name="SmsFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\XXXX\SMS.IncidentBilling.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %-5level %logger %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="NHibernateFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="D:\Dev\SMS\Main\Source\SMS.Website\Logs\nhibernate.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] %appdomain %-5level %c %message %newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
In the console app I have refereneced log4net dll and am declareing amember as
private ILog Log = LogManager.GetLogger(typeof(Task));
Can anyone see anything stupid that I am doing. THere are no actual errors, just nothing is gettign logged via the console app
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
连同 Leniel Macaferi 的回答,网络应用程序与控制台应用程序需要注意的一个区别问题是,如果您为 log4net 使用单独的自定义配置文件(即它不是 App.config 文件的一部分),那么您需要设置文件的属性以在构建和编译时进行复制。否则,它不会找到它。对于 Web 应用程序来说,这通常不是问题,因为它可以在根目录中找到该文件。
要更改它,请在 Visual Studio 中:
在解决方案资源管理器中查找配置文件 ->右键单击该文件->选择属性->复制到输出目录 -> “始终复制”
Along with Leniel Macaferi's answer, one difference to note with a web app versus a console app is that if you are using a separate custom config file for log4net (i.e. it is not part of the App.config file) then you need to set the file's property to be copied when it is built and compiled. Otherwise, it will not find it. This typically is not a problem with a web application since it can find the file within the root directory.
To change it, in Visual Studio:
Find the config file in Solution Explorer -> right-click the file -> select Properties -> Copy to Output Directory -> "Copy always"
我不记得我到底做了什么,但我认为你应该在使用 log4net 之前初始化它。
像这样的行:
请查看此处以获取更多信息:
有log4net 使用应用程序配置文件来获取配置数据。
也请参阅这个(他正在调用
log4net.Config.BasicConfigurator.Configure();
):C# .net 中的 Log4Net 教程(如何在文件中显示日志?)
I don't remember exactly what I did, but I think you should initialize log4net before using it.
Something like this line:
Take a look here for more info:
Have log4net use application config file for configuration data.
See this one too (he's calling
log4net.Config.BasicConfigurator.Configure();
) :Log4Net Tutorial in C# .net (How can I show log in a file?)