更改后 AdoNetAppender 停止工作
我正在使用 log4net,并配置了 XML 文件以使用 AdoNetAppender 登录数据库,当我创建应用程序并配置所有内容时,一切似乎都正常工作。我可以成功登录数据库。但是,当我更改代码中的消息时,它会停止记录到数据库。
这是我的配置:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Server=USER-PC;Integrated Security=SSPI;Initial Catalog=mydb;Trusted_Connection=true;"/>
<commandText value="INSERT INTO Log1 ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<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">
<converter>
<name value="hex_thread" />
<type value="MyWebApplication.HexPatternConverter" />
</converter>
<conversionPattern value="%hex_thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value="50"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level"/>
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger"/>
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message"/>
</layout>
</parameter>
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value="2000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
我在网上找到它并做了一些修改以适合我的应用程序。我想这可能是因为我们在获取参数之前插入到表 Log1 中,也许我真的不知道,因为我是 XML 世界的新手,对此知之甚少。
如果我的代码
private static readonly ILog dblog = LogManager.GetLogger("ADONetAppender");
dblog.Info("logging to db");
第一次工作,然后我像这样更改消息,
dblog.Info("I AM LOGGING TO DB");
dblog.Info("me again");
它将根本不起作用,我的文件附加程序没问题,他们可以接受消息更改,但我的 AdoNetAppender 拒绝这样做。为什么会发生这种情况?
编辑添加:
我的功能非常简单,我有这个类来进行日志记录:
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;using log4net;
using log4net.Config; using log4net.Core;
using log4net.Layout; using System.Text;
using System.IO;
using log4net.Layout.Pattern;
namespace myWebApplication
{
public sealed class HexPatternConverter : PatternLayoutConverter
{
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
long id;
if (long.TryParse(loggingEvent.ThreadName, out id))
{
writer.Write(id.ToString("X"));
}
else
{
writer.Write(loggingEvent.ThreadName);
}
}
}
public class myClass
{
private static readonly ILog secondlog = LogManager.GetLogger("methodB");
private static readonly ILog thirdlog = LogManager.GetLogger("methodC");
private static readonly ILog fourthlog = LogManager.GetLogger("methodD");
private static readonly ILog fifthlog = LogManager.GetLogger("ADONetAppender");
public static int methodA()
{
int a = 0;
return a;
}
public static void methodB()
{
methodA();
secondlog.Info("inside method B");
}
public static void methodC()
{
methodB();
thirdlog.Info("inside method C");
}
public static void methodD()
{
methodC();
fourthlog.Info("inside D");
fifthlog.Info("this is db log");
fifthlog.Info("this is me logging to the db");
}
}
}
每当我进行更改时,文件日志都可以,但我的数据库日志不会写入数据库,因为当我尝试时要检索我的表中的数据,没有任何更改。我不确定我做错了什么。
我的记录器配置
<logger name="ADONetAppender">
<appender-ref ref="ADONetAppender"/>
</logger>
<logger name="methodB">
<appender-ref ref="methodB"/>
</logger>
<logger name="methodC">
<appender-ref ref="methodC"/>
</logger>
<logger name="methodD">
<appender-ref ref="methodD"/>
</logger>
<root>
</root>
最后三个是 RollingFileAppender,无论我更改消息还是添加其他一些消息,它们都可以完美工作。当我更改消息或添加更多消息来记录时,数据库让我头疼。
I'm using log4net and I configured my XML file to log into database with AdoNetAppender and everything seems to work properly when I create the application and configure everything. And I can successfully log to the database. But when I change the message in my code then it stops logging to the database.
Here is my configuration:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Server=USER-PC;Integrated Security=SSPI;Initial Catalog=mydb;Trusted_Connection=true;"/>
<commandText value="INSERT INTO Log1 ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<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">
<converter>
<name value="hex_thread" />
<type value="MyWebApplication.HexPatternConverter" />
</converter>
<conversionPattern value="%hex_thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value="50"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level"/>
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger"/>
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message"/>
</layout>
</parameter>
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value="2000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
I found it online and did a little modification to suit my application. I'm thinking it could be because we insert into the table Log1 before getting the parameters MAYBE I really don't know because I am new to XML world and I know very little about it.
If my code is
private static readonly ILog dblog = LogManager.GetLogger("ADONetAppender");
dblog.Info("logging to db");
it works the first time, and then I change the message like this
dblog.Info("I AM LOGGING TO DB");
dblog.Info("me again");
it will not work at all, my file appenders are all right they can take the message change but my AdoNetAppender refuses this. Why is this happening?
Edited to add:
My functions are very simple I have this class to do the logging:
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;using log4net;
using log4net.Config; using log4net.Core;
using log4net.Layout; using System.Text;
using System.IO;
using log4net.Layout.Pattern;
namespace myWebApplication
{
public sealed class HexPatternConverter : PatternLayoutConverter
{
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
long id;
if (long.TryParse(loggingEvent.ThreadName, out id))
{
writer.Write(id.ToString("X"));
}
else
{
writer.Write(loggingEvent.ThreadName);
}
}
}
public class myClass
{
private static readonly ILog secondlog = LogManager.GetLogger("methodB");
private static readonly ILog thirdlog = LogManager.GetLogger("methodC");
private static readonly ILog fourthlog = LogManager.GetLogger("methodD");
private static readonly ILog fifthlog = LogManager.GetLogger("ADONetAppender");
public static int methodA()
{
int a = 0;
return a;
}
public static void methodB()
{
methodA();
secondlog.Info("inside method B");
}
public static void methodC()
{
methodB();
thirdlog.Info("inside method C");
}
public static void methodD()
{
methodC();
fourthlog.Info("inside D");
fifthlog.Info("this is db log");
fifthlog.Info("this is me logging to the db");
}
}
}
The file logs are okay whenever I make changes but my DB log isn't writing to the database, because when I try to retrieve the data in my table there are no changes. I am not sure what I am doing wrong.
My Logger Configuration
<logger name="ADONetAppender">
<appender-ref ref="ADONetAppender"/>
</logger>
<logger name="methodB">
<appender-ref ref="methodB"/>
</logger>
<logger name="methodC">
<appender-ref ref="methodC"/>
</logger>
<logger name="methodD">
<appender-ref ref="methodD"/>
</logger>
<root>
</root>
The last three are RollingFileAppender
s and they work perfectly wether I change the message or I add a few other messages. It's the DB that's giving me headache when I change the message or I add a few more messages to log.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一:将缓冲区大小更改为 1,而不是 0。
第二:如果第一条日志消息有效,则您的配置文件没有问题。它可能在您的其余代码中,请向我们展示更多内容。
如果很复杂,请使用一两个非常简单的函数进行测试,然后让我们知道会发生什么。
编辑添加
好的,我看到了你的代码,现在我看到
HexPatternConverter
被用来格式化一列:所以那里可能出了问题,比如抛出异常。我建议您尝试在不使用该类的情况下运行它(只需将 Thread 列写为纯字符串),看看是否会改变任何内容。另外,以防万一您不知道:如果 log4net 遇到 SQL 问题,它会默默地失败。在这种情况下这似乎不太可能,因为它至少写入了几条记录,但仍然如此。
编辑添加
当您更改配置文件时,IIS 会重新加载整个应用程序,因此您可能会耗尽资源。您还有其他数据库活动吗?也停下来吗?您是否有可能打开数据库连接并忘记关闭它们?这将导致数据库活动失败,直到应用程序重新启动。
编辑添加
配置文件中的 ConnectionString 不会打开连接,它只是您的程序(或在本例中为 log4net)知道要查找的位置。如果除了 log4net 之外您没有执行任何数据库活动,那么这可能不是连接问题。
接下来要尝试的事情:log4net 的文本文件 Appender 是否存在此问题?
First: change the buffer size to 1, not 0.
Second: if the first log message is working, your config file is fine. It's probably in the rest of your code, show us a little more.
If it's complicated, test with a very simple function or two then let us know what happens.
Edited to add
Okay, I see your code, and now I see that
HexPatternConverter
is being used to format one column: so something's probably going wrong there, such as throwing an exception. I suggest you try running this without using that class (just writing the Thread column as a plain string) and see if that changes anything.Also, just in case you didn't know: if log4net encounters a SQL problem, it will fail silently. That seems unlikely in this case, because it's writing at least a couple of records, but still.
Edited to add
When you change the config file, IIS reloads the whole application, so maybe you're running out of resources. Do you have other database activity? Does it stop as well? Is it possible you're opening db connections and forgetting to close them? That would cause db activity to fail until the app restarts.
Edited to add
The ConnectionString in the config file doesn't open connections, it's just a place your program (or in this case log4net) knows to look. If you're not doing any DB activity besides log4net then it's probably not a connection problem.
Next thing to try: does log4net have this problem with a text file Appender?
噢,最后,它运行得很好...我在 global.asax 中包含了一些我认为不应该存在的代码。我刚刚尝试修改代码,效果很好。现在,无论我做了多少更改,它都会立即写入。
这是代码,我不知道它的作用,但我只是在网上找到它。我的情况不需要它
,在我删除它之后,问题解决了。
OOH finally, its worked well... There is some code that I included in my global.asax that shouldnt be there i believe. I just tried revoming the code and it worked well. Now its instantly writing no matter how many changes i make.
Here is the code i have no idea what it does but i just found it online. It wasnt needed in my case
And after i removed it Problem solved.