日志文件未创建?
我正在使用 log4net 并已使用 param name="File" value= "C:\Application.log" 完全设置它。但该文件并未在 C: 中创建。我运行的是 Windows 7,也许权限之类的东西阻止了文件的创建。
这是app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>`
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender“ type=“log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Users\Mohit\Documents\Application.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern“ value=“%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
</configuration>
I am using log4net and have completely setup it up with param name="File" value= "C:\Application.log". Yet the file is not created in C:. I am running Windows 7 and maybe something like permissions is preventing the file from being created.
Here is the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>`
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender“ type=“log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Users\Mohit\Documents\Application.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern“ value=“%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须提供真实的文件名。您在配置中定义的是文件夹名称。而不是:
使用类似的内容:
另外,您可能需要提升应用程序的权限才能将日志写入根 c: 文件夹。 UAC 不会让您写入根文件夹。
就像 Andy 所说,您最好选择 Windows Users 文件夹的一些子文件夹,例如:
log4net 有一些预定义的变量,您可以使用它们来定位特殊文件夹。这里有一些问题:
如何指定通用log4net 的应用程序数据文件夹?
C#如何在app.config文件中指定appData文件路径
You must provide a real file name. What you defined within your config is a folder name. Instead of:
use something like:
Also, you'll probably need elevated permissions for your application to write the log to the root c: folder. UAC won't let you write to root folder.
Like Andy said, you'll be better to choose some subfolder of Windows Users folder like:
log4net has some predefined variables you can use to target special folders. There are some questions about that here on SO:
How to specify common application data folder for log4net?
C# how to specify the appData file path in the app.config file
是的,请确保正在执行该应用程序的用户具有对 c: 的写入权限。
更好的是,您可能不想将应用程序日志写入根 c:\ 目录。最好选择应用程序的安装位置,或者“文档和设置”(或 Windows 7 等效项)下的某个位置。
Yeah, make sure the user that is executing the application has write permissions to c:.
Better yet, you probably don't want to write your application log to the root c:\ directory. It would probably be better to choose a location where your app is installed, or somewhere under Documents and Settings (or the Windows 7 equivalent).
我的问题是 App.config 文件中各部分的顺序。我首先有我的
部分,然后是我的
。由于某种原因,我的 Windows 应用程序中没有出现错误,但控制台应用程序中确实出现了错误。显然
必须是
下的第一部分所以,而不是这样:
这样做:
My problem was the order of sections in my App.config file. I had my
<startup>
section first, then my<configSections>
. For some reason I wasn't getting an error in my Windows app, but it did give an error in a Console app. Apparently<configSections>
must be the first section under<configuration>
So, instead of this:
Do this:
解决我的问题的基本上是 CTBrewski 在这里发布的内容(+1 顺便说一句!),但我的 App.config 有一个 appSettings 条目不是 configSections 条目。
我将 appSettings 条目和 log4net 配置条目移至启动条目上方,然后将日志写入用户配置文件:
当然,我的附加程序如下所示:
What solved my problem was basically what CTBrewski posted here (+1 btw!), but my App.config had an appSettings entry not a configSections entry.
I moved the appSettings entry with my log4net config entries above the startup entry, and the logs were then written to the user profile:
And then of course my appender looks like this: