我假设的一点是小事情正在阻止我的应用程序进行数据库日志记录。
INSERT
和EXEC
命令都工作,数据输入数据库ExecutionTime
参数,它就会停止工作。StoredProcedure
它会停止工作,即使文本版本正常工作SQL:
DROP TABLE [LOG];
GO
CREATE TABLE [dbo].[LOG] (
[Id] [int] IDENTITY(1,1) NOT NULL
,[Origin] [varchar](55) null
,[LogDate] [datetime] NOT NULL
,[Thread] [varchar](32) NOT NULL
,[Context] [varchar](10) NOT NULL
,[Level] [varchar](10) NOT NULL
,[Logger] [varchar](255) NOT NULL
,[Message] [varchar](4000) Not NULL
,[MethodName] [varchar](200) NULL
,[Parameters] [varchar](4000) NULL
,[Exception] [varchar](4000) NULL
,[ExecutionTime] [decimal](14, 4) NULL
)
GO
DROP PROCEDURE InsertLog
GO
CREATE PROCEDURE [dbo].[InsertLog]
@LogDate DateTime
,@Thread varchar(32)
,@Context varchar(10)
,@Level varchar(10)
,@Logger varchar(255)
,@Message varchar(4000)
,@MethodName varchar(200) = null
,@Parameters varchar(4000) = null
,@Exception varchar(4000) = null
,@ExecutionTime decimal(14,4) = null
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[LOG] ([Origin],[LogDate],[Thread],[Level],[Logger],[Message],[MethodName],[Parameters],[Exception],[Context])
VALUES ('InsertLog',@LogDate, @Thread, @Level, @Logger, @Message, @MethodName, @Parameters, @Exception, @Context)
END
GO
的App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
...
</configSections>
<connectionStrings>...</connectionStrings>
<applicationSettings>...</applicationSettings>
<log4net configSource="Log4Net.config" />
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData=".\log4net.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<!-- ColoredConsoleAppender works, so It's not listed below -->
<appender-ref ref="ColoredConsoleAppender"/>
<appender-ref ref="ADONetAppender" />
<appender-ref ref="OutputDebugStringAppender" />
<appender-ref ref="TraceAppender" />
</root>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<threshold value="ALL"/>
<bufferSize value="1" />
<lossy value="false"/>
<param name="UseTransactions" value="False" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="..." />
<!--<commandText value="
INSERT INTO [Log] ([Origin],[LogDate],[Thread],[Context],[Level],[Logger],[Message],[MethodName],[Parameters],[Exception])
VALUES ('ADONetAppender',@log_date,@thread,@Context,@Level,@Logger,@Message,@MethodName,@Parameters,@Exception);"
/>-->
<commandText value="
exec InsertLog @log_date,@thread,@Context,@Level,@Logger,@Message,@MethodName,@Parameters,@Exception;"
/>
<!--<commandType value="StoredProcedure" />-->
<!--<commandText value="InsertLog" />-->
<parameter name="LogDate">
<parameterName value="@log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
...
<!-- This causes appender to fail silently. -->
<parameter name="ExecutionTime">
<parameterName value="@ExecutionTime"/>
<dbType value="Decimal"/>
<precision value="14"/>
<scale value="4"/>
<layout type="log4net.Layout.PatternLayout" value="%property{execution_time}" />
</parameter>
</appender>
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
</log4net>
更新1 现在我有Trace的东西正在工作......它抱怨DB命令需要明确设置精度和比例。不知道怎么做,因为我见过的所有例子都没有设置
log4net:错误[AdoNetAppender] ErrorCode:GenericFailure。不能 prepare database命令[exec InsertLog @ LOG_DATE,@螺纹,@上下文,@等级,@记录器,@消息,@方法名,@参数,@异常; ] System.InvalidOperationException:SqlCommand.Prepare方法需要 类型的参数&#39;十进制&#39;有一个明确设置精度和 规模。在System.Data.SqlClient.SqlParameter.Prepare(SqlCommand cmd)在System.Data.SqlClient.SqlCommand.Prepare()at log4net.Appender.AdoNetAppender.InitializeDatabaseCommand()
更新2 随着Precision / Scale的增加,错误会发生变化。它现在抱怨:
log4net:错误[AdoNetAppender] ErrorCode:GenericFailure。 DoAppend失败 System.FormatException:无法将参数值从String转换为Decimal。 ---&GT; System.FormatException:输入字符串的格式不正确。
答案 0 :(得分:3)
有两个参数属于AdoNetAppenderParameter
类,可以添加到xml文件ExecutionTime
和Precision
中的Scale
参数中:
<parameter name="ExecutionTime">
<parameterName value="@ExecutionTime"/>
<dbType value="Decimal"/>
<precision value="14"/>
<scale value="4"/>
<layout type="log4net.Layout.PatternLayout" value="%property{execution_time}" />
</parameter>
答案 1 :(得分:1)
(null)
,并且在将其翻译成小数时会窒息。<强> SQL:强>
DROP TABLE [LOG];
GO
CREATE TABLE [dbo].[LOG] (
[Id] [int] IDENTITY(1,1) NOT NULL
,[LogDate] [datetime] NOT NULL
,[Thread] [varchar](32) NOT NULL
,[Level] [varchar](10) NOT NULL
,[Logger] [varchar](255) NOT NULL
,[Message] [varchar](4000) NOT NULL
,[Exception] [varchar](4000) NULL
,[Context] [varchar](10) NULL
,[MethodName] [varchar](200) NULL
,[Parameters] [varchar](4000) NULL
,[ExecutionTime] int NULL
--,[Origin] [varchar](55) NOT NULL
--,[ExecutionRaw] [varchar](4000) NULL
)
GO
DROP PROCEDURE InsertLog
GO
CREATE PROCEDURE [dbo].[InsertLog]
@LogDate DateTime
,@Thread varchar(32)
,@Level varchar(10)
,@Logger varchar(255)
,@Message varchar(4000)
,@Exception varchar(4000) = null
,@Context varchar(10) = null
,@MethodName varchar(200) = null
,@Parameters varchar(4000) = null
,@ExecutionTime varchar(32) = null
AS
BEGIN
SET NOCOUNT ON;
--DECLARE @ETChar varchar(32); set @ETChar = @ExecutionTime;
if @Exception = '' set @Exception = null;
if @Context = '(null)' set @Context = null;
if @MethodName = '(null)' set @MethodName = null;
if @Parameters = '(null)' set @Parameters = null;
if @ExecutionTime = '(null)' set @ExecutionTime = null;
DECLARE @ETInt int; set @ETInt = convert(int, @ExecutionTime);
INSERT INTO [dbo].[LOG] (
[LogDate]
,[Thread]
,[Level]
,[Logger]
,[Message]
,[Exception]
,[Context]
,[MethodName]
,[Parameters]
,[ExecutionTime]
--,[Origin]
--,[ExecutionRaw]
)
VALUES (
@LogDate
,@Thread
,@Level
,@Logger
,@Message
,@Exception
,@Context
,@MethodName
,@Parameters
,@ETInt
--,'InsertLog'
--,@ETChar
)
SET NOCOUNT OFF;
END
GO
DROP INDEX [IX_LEVEL] ON [LOG]
GO
CREATE NONCLUSTERED INDEX [IX_Level] ON [dbo].[LOG] ( [Level] ASC );
GO
<强> Log4Net.config:强>
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<threshold value="ALL" />
<bufferSize value="1" />
<lossy value="false" />
<param name="UseTransactions" value="False" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="..." />
<commandType value="StoredProcedure" />
<commandText value="InsertLog" />
<parameter name="LogDate">
<parameterName value="@LogDate" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter name="Thread">
<parameterName value="@Thread" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter name="Level">
<parameterName value="@Level" />
<dbType value="String" />
<size value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter name="Logger">
<parameterName value="@Logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter name="Message">
<parameterName value="@Message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter name="Exception">
<parameterName value="@Exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter name="Context">
<parameterName value="@Context" />
<dbType value="String" />
<size value="10" />
<layout type="log4net.Layout.PatternLayout" value="%x" />
</parameter>
<parameter name="MethodName">
<parameterName value="@MethodName" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout" value="%property{method_name}" />
</parameter>
<parameter name="Parameters">
<parameterName value="@Parameters" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%property{properties}" />
</parameter>
<parameter name="ExecutionTime">
<parameterName value="@ExecutionTime" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout" value="%property{execution_time}" />
</parameter>
</appender>
</log4net>