简体   繁体   English

Log4Net没有写入数据库

[英]Log4Net not writing to the database

I've checked the connection string (I got it from the server explorer). 我检查了连接字符串(我从服务器资源管理器中获取)。

I've checked the commandText in log4net config. 我在log4net config中检查了commandText。

I've checked the database permissions (integrated security is fine and works outside of the log4net class). 我已经检查了数据库权限(集成安全性很好,并且在log4net类之外工作)。

I've checked the repository's configured property (it is configured, it finds the config file fine). 我已经检查了存储库的配置属性(它已配置,它找到配置文件正常)。

I've also checked that the fields defined in the config file match the attributes (field size etc.) of the table in the database. 我还检查了配置文件中定义的字段是否与数据库中表的属性(字段大小等)相匹配。

Any ideas? 有任何想法吗?

When I'm debugging it seems to be hitting all the right methods at all the right times, with no exceptions raised. 当我正在调试它似乎在所有正确的时间点击所有正确的方法,没有异常提出。

<log4net>

  <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="" />
    <commandText value="INSERT INTO dbo.Log4Net ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('01-01-2001', 'test', 'test', 'test', 'test')"/>
    <!--<commandText value="INSERT INTO dbo.Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MachineName],[CultureId],[SourcePage],[Details],[Method]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @MachineName, @CultureId, @SourcePage, @Details, @Method)" />-->
    <parameter>
      <parameterName value="@log_date"/>
      <dbType value="DateTime"/>
      <layout type="log4net.Layout.RawTimeStampLayout"/>
    </parameter>
    <parameter>
      <parameterName value="@thread"/>
      <dbType value="String"/>
      <size value="255"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread"/>
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level"/>
      <dbType value="String"/>
      <size value="50"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level"/>
      </layout>
    </parameter>

...more parameters

    <securitycontext type="log4net.Util.WindowsSecurityContext">
      <credentials value="Process">
      </credentials>
    </securitycontext>
  </appender>

  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="LogTest.txt"/>
    <param name="AppendToFile" value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n"/>
    </layout>
  </appender>

  <root>
    <appender-ref ref="ADONetAppender"/>
    <appender-ref ref="FileAppender"/>
  </root>

</log4net>

It's writing to neither appender. 它写的都不是appender。

Right, after hours of pulling my hair out - I've cracked it. 好吧,经过几个小时的拔毛 - 我已经破了。

This line: 这一行:

log4net.Config.XmlConfigurator.Configure();

Needed putting in prior to any logging (well, as early as possible in the app). 在任何日志记录之前需要输入(好吧,尽早在应用程序中)。 That's it. 而已。 That was all it took. 这就是全部。 This is one of those problems were I'm extremely relieved but frustrated at the same time. 这是其中一个问题,我很放心,但同时感到沮丧。

I would recommend turning on Log4Net debugging: 我建议打开Log4Net调试:

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

That may point you in the right direction if there's an error that's occurring behind the scenes. 如果在幕后发生错误,这可能会指向正确的方向。 The output will be directed to the console output in the IDE or in the command line. 输出将定向到IDE或命令行中的控制台输出。

  1. Check if log4net.dll is placed in the same folder as your application. 检查log4net.dll是否与应用程序位于同一文件夹中。
  2. Try to enable log4net self-logging, maybe it'll help to find out: 尝试启用log4net自我记录,也许它有助于找出:

     <configuration> <appSettings> <add key="log4net.Internal.Debug" value="true"/> </appSettings> <system.diagnostics> <trace autoflush="true"> <listeners> <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\\tmp\\log4net.txt" /> </listeners> </trace> </system.diagnostics> </configuration> 

See also the official log4net FAQ . 另请参阅官方log4net常见问题解答

per the ADONetAppender config example: 根据ADONetAppender配置示例:

<commandText value="INSERT INTO dbo.Log4Net 
    ([Date],[Thread],[Level],[Logger],[Message]) 
    VALUES (@log_date, @thread, @log_level, @logger, @message)"/>

This uses the ADO.NET parameterized query format, so you need to use that syntax. 这使用ADO.NET参数化查询格式,因此您需要使用该语法。 Additionally, you may not want to use integrated security for the db connection (esp. if you are running a web site or a service). 此外,您可能不希望为数据库连接使用集成安全性(例如,如果您正在运行网站或服务)。 For your file appender, I recommend a fully qualified path, and make sure it is writeable by the logger. 对于您的文件追加器,我建议使用完全限定的路径,并确保它可由记录器写入。

I assume you have already created the table in the specified database? 我假设您已经在指定的数据库中创建了表?

NOTE I recommend setting the trace appender in Debug mode too, to make sure you are actually logging stuff. 注意我建议也在调试模式下设置跟踪追加器 ,以确保您实际记录的是东西。

I ran into a similar issue yesterday, Log4Net was just not writing to the database. 昨天我遇到了类似的问题,Log4Net只是没有写入数据库。 I copied the configuration from another existing appender that successfully writes logs to the database. 我从另一个成功将日志写入数据库的现有appender复制了配置。 My solution was to run SQL Server Profiler to try and catch what was happening. 我的解决方案是运行SQL Server Profiler来尝试捕获正在发生的事情。 Profiler showed that the INSERT statements were being sent by Log4Net, but it was failing on the SQL Server side. Profiler显示INSERT语句是由Log4Net发送的,但它在SQL Server端失败了。 Manually running the INSERT statement in SQL Server Management Studio showed me exactly what was wrong with it, in my case, it was inserting NULL into a column that didn't accept NULL. 在SQL Server Management Studio中手动运行INSERT语句向我显示了它究竟出了什么问题,在我的例子中,它将NULL插入到不接受NULL的列中。

Add this line in the AssemblyInfo.cs file 在AssemblyInfo.cs文件中添加此行

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

As the official configuration manual states "The log4net configuration can be configured using assembly-level attributes rather than specified programmatically", which I found as a more clear approach. 正如官方配置手册所述“可以使用汇编级属性配置log4net配置而不是以编程方式指定”,我发现这是一种更清晰的方法。 Some people might find my answer as a more straightforward. 有些人可能会觉得我的答案更简单。

Source: https://logging.apache.org/log4net/release/manual/configuration.html 资料来源: https//logging.apache.org/log4net/release/manual/configuration.html

for sql server connectivity I'm using my live account to login to azure and sql server and due to that I had to change integrated security to "SSPI" instead of "true" did the trick 对于sql server连接我正在使用我的真实帐户登录到azure和sql server,因为我必须将集成安全性更改为“SSPI”而不是“true”才行

only found out it was the con string by adding this 只是通过添加它才发现它是con字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM