NLog - 数据库配置 - 德语SQL Server上的例外 - 任何想法如何解决?

kmp*_*kmp 6 .net c# nlog

背景

我使用的是NLog版本2.0.1,我相信它是最新版本,来自.Net 4.0控制台应用程序(使用Visual Studio 2012编写).

该应用程序很简单:

namespace NLogConsoleApplication
{
    class Program
    {
        static void Main()
        {
            NLog.LogManager.GetCurrentClassLogger().Info("A testypoos");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下NLog.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true"
      autoReload="true"  
      internalLogFile="C:\Users\Public\nlog.txt"
      internalLogLevel="Debug">
  <targets>    
    <target type="Database" name="tl" >
        <connectionString>data source=.\;initial catalog=NLogTest;integrated security=false;User ID=iwillnotfallforthisone;Password=goodtry</connectionString>
        <commandText>
          insert into TestLog2 ([LogDate], [LogLevel], [LogMessage]) values (@logDate, @logLevel, @logMessage);
        </commandText>
        <parameter name="@logDate" layout="${date}"/>
        <parameter name="@logLevel" layout="${level}"/>
        <parameter name="@logMessage" layout="${message}"/>
    </target>
  </targets>
  <rules>    
    <logger name="*" minlevel="Trace" writeTo="tl" />
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

TestLog2表的创建方式如下:

CREATE TABLE [dbo].[TestLog2](
    [LogId] [bigint] IDENTITY(1,1) NOT NULL,
    [LogDate] [datetime] NOT NULL,
    [LogLevel] [nvarchar](20) NOT NULL,
    [LogMessage] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_TestLog2] PRIMARY KEY CLUSTERED 
(
    [LogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我得到了这个例外(我谷歌翻译了这个 - 希望它是可以理解的):

Unhandled Exception: NLog.NLogRuntimeException: Exception occurred in NLog --- > System.Data.SqlClient.SqlException:During the conversion of a nvarchar data type to a datetime data type, the value is out of range.
The statement has been terminated.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)
   at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
   --- End of inner exception stack trace ---
   at NLog.LoggerImpl.<>c__DisplayClass1.<Write>b__0(Exception ex)
   at NLog.Internal.SingleCallContinuation.Function(Exception exception)
   at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
   at NLog.Targets.Target.WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
   at NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException)
   at NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
   at NLog.Logger.Info(String message)
   at NLogConsoleApplication.Program.Main() in c:\xxxxxxx\Program.cs:Line 7.
Run Code Online (Sandbox Code Playgroud)

如果我将LogDate列更改为nvarchar它工作正常但我真的不希望该列是一个字符串,因为我希望能够根据日期对表进行排序和搜索.

笔记

  • 我在连接到德语版SQL Sever 2012的德语版Windows Server 2008 R2标准上运行此程序.
  • 如果我连接到英语SQL Server 2012实例,它工作正常.
  • 我检查了nlog.txt文件,似乎没有进一步的信息(只是相同的异常跟踪)

也许它只是NLog中的一个错误,或者它只是一个我可以更改的配置或者有一个聪明的解决方法,所以,有谁知道我将如何让NLog将DateTime时间戳放入我的数据库?

Ed *_*pel 7

我猜想datetime来自NLog 的格式是MM/dd/yyyy德国SQL Server所期望的dd/MM/yyyy.您可以使用以下命令明确指定日期格式:

<parameter name="@logDate" layout="${date:format=yyyy-MM-dd}"/>
Run Code Online (Sandbox Code Playgroud)

SQL Server应该能够解析日期并适当地插入它,而不管语言环境是什么MM/dd或期望的dd/MM.

一个类似的问题

NLog文档

  • 啊,非常好 - 这就是谢谢 - 我指定了SQL服务器如何喜欢日期时间:<parameter name ="@ logDate"layout ="$ {date:format = yyyy-MM-ddTHH \:mm \:ss. fff}"/>参数,它工作. (2认同)