日志文件未创建?

发布于 2024-08-17 07:22:54 字数 1280 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〗斷ホ乔殘χμё〖 2024-08-24 07:22:54

您必须提供真实的文件名。您在配置中定义的是文件夹名称。而不是:

<param name="File" value="C:\Users\Mohit\Documents" />

使用类似的内容:

<param name="File" value="C:\Users\Mohit\Documents\log.txt" />

另外,您可能需要提升应用程序的权限才能将日志写入根 c: 文件夹。 UAC 不会让您写入根文件夹。

就像 Andy 所说,您最好选择 Windows Users 文件夹的一些子文件夹,例如:

c:\Users\Mohit\AppData\Local\<MyApplication>

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:

<param name="File" value="C:\Users\Mohit\Documents" />

use something like:

<param name="File" value="C:\Users\Mohit\Documents\log.txt" />

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:

c:\Users\Mohit\AppData\Local\<MyApplication>

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

总攻大人 2024-08-24 07:22:54

是的,请确保正在执行该应用程序的用户具有对 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).

小女人ら 2024-08-24 07:22:54

我的问题是 App.config 文件中各部分的顺序。我首先有我的 部分,然后是我的 。由于某种原因,我的 Windows 应用程序中没有出现错误,但控制台应用程序中确实出现了错误。显然 必须是 下的第一部分

所以,而不是这样:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

这样做:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

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:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

Do this:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
轻许诺言 2024-08-24 07:22:54

解决我的问题的基本上是 CTBrewski 在这里发布的内容(+1 顺便说一句!),但我的 App.config 有一个 appSettings 条目不是 configSections 条目。

我将 appSettings 条目和 log4net 配置条目移至启动条目上方,然后将日志写入用户配置文件:

<configuration>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
    <add key="log4net.Config.Watch" value="True" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings> 
  <startup>
    <supportedRuntime version="v2.0.50727" />
  </startup>
  ...
  ...

当然,我的附加程序如下所示:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="${LOCALAPPDATA}/Synclio/Logs/SynclioWin.log" />
    <appendToFile value="true" />
    <maximumFileSize value="5000KB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
  </appender>

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:

<configuration>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
    <add key="log4net.Config.Watch" value="True" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings> 
  <startup>
    <supportedRuntime version="v2.0.50727" />
  </startup>
  ...
  ...

And then of course my appender looks like this:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="${LOCALAPPDATA}/Synclio/Logs/SynclioWin.log" />
    <appendToFile value="true" />
    <maximumFileSize value="5000KB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
  </appender>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文