Log4Net 不记录或错误

发布于 2024-07-26 03:02:27 字数 3013 浏览 2 评论 0原文

我一直在尝试在我的 ASP.NET Web 应用程序中获取 log4net 日志记录,但没有成功或任何明显的错误。 我尝试使用具有以下配置的 ADONetAppender 附加程序:

<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="server=" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Context]) VALUES 
                            (@log_date, @thread, @log_level, @logger, @message, @exception, @context)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="32" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%t" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%p" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@context" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%x" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%c" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%m" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ADONetAppender" />
</root>

在我的 global.asax Application_Start 中,我调用

log4net.Config.XmlConfigurator.Configure();

Then 来尝试记录:

protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{            
    log.Info("did I log yet?");
}

据我所知,所有这些都没有执行任何操作。

I have been attempting to get log4net logging in my asp.net web application with no success or any noticable errors. I am attempting to use the ADONetAppender appender with the following config:

<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="server=" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Context]) VALUES 
                            (@log_date, @thread, @log_level, @logger, @message, @exception, @context)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="32" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%t" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%p" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@context" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%x" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%c" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%m" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ADONetAppender" />
</root>

In my global.asax Application_Start I call

log4net.Config.XmlConfigurator.Configure();

Then to attempt to log:

protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{            
    log.Info("did I log yet?");
}

All of this does nothing as far as I can tell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

仅此而已 2024-08-02 03:02:28

log4Net 的内部调试是@Pratik 建议的方法。 要将详细信息写入文件,请执行以下操作:

即使您为 log4net 使用不同的配置文件,也请在 web.config 中添加以下内容。

<appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

<system.diagnostics>
<trace autoflush="true">
  <listeners>
    <add
    name="textWriterTraceListener"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="C:\toolbox\SmartClient\log4net.txt" />
  </listeners>
</trace>
</system.diagnostics>

参考

  1. 如何启用 log4net 内部调试?
  2. log4net - Appender 无法在 IIS7.5 中工作

Internal debugging for log4Net is the way to go as suggested by @Pratik. To write the details into a file do the following:

Add the following in web.config even if you are using a different config file for log4net.

<appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

<system.diagnostics>
<trace autoflush="true">
  <listeners>
    <add
    name="textWriterTraceListener"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="C:\toolbox\SmartClient\log4net.txt" />
  </listeners>
</trace>
</system.diagnostics>

Reference

  1. How do I enable log4net internal debugging?
  2. log4net - Appenders not working in IIS7.5
枫以 2024-08-02 03:02:28

您是否在 Configuration->Configsections 下添加了以下部分

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

虽然可以将 log4net 配置设置添加到项目的 app.config 或 web.config 文件中,但最好将它们放在单独的配置文件中。 除了可维护性方面的明显好处之外,它还有一个额外的好处,即 log4net 可以在配置文件上放置一个 FileSystemWatcher 对象来监视它何时更改并动态更新其设置。

要使用单独的配置文件,请将名为 Log4Net.config 的文件添加到您的项目中,并将以下属性添加到 AssemblyInfo.cs 文件中:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

注意:对于 Web 应用程序,假设 Log4Net.config 位于 Web 根目录中。 确保 log4net.config 文件被标记为“复制到输出”-> 属性中的“始终复制”。

来自此处

Have you added added the following section under under Configuration->Configsections

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

Although it is possible to add your log4net configuration settings to your project’s app.config or web.config file, it is preferable to place them in a separate configuration file. Aside from the obvious benefit of maintainability, it has the added benefit that log4net can place a FileSystemWatcher object on your config file to monitor when it changes and update its settings dynamically.

To use a separate config file, add a file named Log4Net.config to your project and add the following attribute to your AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

Note: for web applications, this assumes Log4Net.config resides in the web root. Ensure the log4net.config file is marked as “Copy To Output” -> “Copy Always” in Properties.

From here.

夏日浅笑〃 2024-08-02 03:02:27

您还可以设置以下应用程序设置以启用内部 log4net 调试信息

<appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

内部调试消息将写入控制台(如果有的话)或跟踪(例如 Visual Studio 中的调试窗口或 sysinternals 中的 dbgview)。 这将帮助您解决 log4net 问题。

you can also set the following appsettings to enable internal log4net debugging information

<appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

The internal debug messages are written to the console (if you have one) or the trace (such as the debug window in visual studio or dbgview from sysintenals). This will help you to troubleshoot log4net issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文