SQL Server的Nlog配置

时间:2016-01-12 07:58:26

标签: c# sql-server nlog

我试图通过MVC在SQL Server上插入日志,这是我的配置文件:

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      throwExceptions="false" >
    <targets>
        <target xsi:type="File" name="file" fileName="D:\${shortdate}.log"
             layout="${longdate} ${uppercase:${level}} ${message}" />
        <target xsi:type="Database"
                name="database"
                connectionString="Data Source=*;Initial Catalog=*;User ID=*,Password=*"
                commandType="StoredProcedure"
                commandText="[dbo].[InsertLog]">
            <parameter name="@time_stamp" layout="${longdate}"/>
            <parameter name="@level" layout="${level}"/>
            <parameter name="@logger" layout="${logger}"/>
            <parameter name="@message" layout="${message}"/>
            <parameter name="@machineName" layout="${machinename}"/>
            <parameter name="@userName" layout="${windows-identity:domain=true}"/>
            <parameter name="@callSite" layout="${callsite:filename=true}"/>
            <parameter name="@threadId" layout="${threadid}"/>
            <parameter name="@logException" layout="${exception}"/>
            <parameter name="@stackTrace" layout="${stacktrace}"/>
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="database,file" />
    </rules>
</nlog>

这是我的存储过程:

Create Procedure [dbo].[InsertLog]
(
    @time_stamp datetime,
    @level nvarchar(MAX),
    @logger nvarchar(MAX),
    @message nvarchar(MAX),
    @machineName nvarchar(MAX),
    @userName nvarchar(MAX),
    @callSite nvarchar(MAX),
    @threadId nvarchar(MAX),
    @logException nvarchar(MAX),
    @stackTrace nvarchar(MAX)
)
AS
INSERT INTO dbo.Genel
(
      [time_stamp]
      ,[level]
      ,[logger]
      ,[message]
      ,[machineName]
      ,[userName]
      ,[callSite]
      ,[threadId]
      ,[logException]
      ,[stackTrace]
)
VALUES
(
       @time_stamp
      ,@level
      ,@logger
      ,@message
      ,@machineName
      ,@userName
      ,@callSite
      ,@threadId
      ,@logException
      ,@stackTrace
)

最后,我的代码背后:

private static Logger logger = LogManager.GetCurrentClassLogger( );
...
logger.Info("ReadIL_PRM is entered!");
...
logger.Info("IL_PRM is returned!");
...

我可以写入txt文件但无法插入SQL Server。怎么了?提前致谢。对不起,我的郎。

0 个答案:

没有答案