问题
I use log4net
for logging errors in my project. I want to log messages into DB (SQL Server
) so I added AdoNetAppender
but it does not work (other appenders work fine, connection string is correct).
What can be wrong?
回答1:
I decided to create a bare-bones example project. This works. Perhaps you should try making it work.
Create an empty console application project. Add a reference to log4net. C# Code:
using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Litter
{
class Program
{
static void Main()
{
LogManager.GetLogger("default").Info("Hello, World!");
}
}
}
Config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <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="data source=localhost\sqlexpress;initial catalog=Litter;integrated security=True;"/> <commandText value="INSERT INTO Logs ([Message]) VALUES (@message)"/> <parameter> <parameterName value="@message"/> <dbType value="String"/> <size value="2000"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message"/> </layout> </parameter> </appender> <root> <level value="DEBUG"/> <appender-ref ref="AdoNetAppender"/> </root> </log4net> </configuration>
Database table:
CREATE TABLE [dbo].[Logs]([Message] [nvarchar](2000) NOT NULL) GO
That's about as simple as it gets. If you can make this work, then I'd start looking very closely at your app's AdoNetAppender configuration.
回答2:
Thank you all. The problem was in DB. I just have to set the RowGuid
property as true.
来源:https://stackoverflow.com/questions/18257801/logging-into-db-with-log4net