使用 NLOG 插入指南

发布于 2024-11-27 19:27:11 字数 2462 浏览 0 评论 0原文

出现错误。

Exception occurred in NLog ---> System.Data.SqlClient.SqlException: Conversion failed when converting from a character string to uniqueidentifier.

当我尝试在可以采用 NULL 的表的 UserId 列中插入 NULL 时,

<?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" internalLogFile="C:\Development\Projects\Pearson\ARIES\Development\Websites\ARIES.Web\NLog.log" internalLogLevel="Debug" >

    <!-- make sure to set 'Copy To Output Directory' option for this file -->
    <!-- go to http://nlog-project.org/wiki/Configuration_file for more information -->

    <targets>
        <target xsi:type="AsyncWrapper" queueLimit="2000" overflowAction="Grow" name="AsyncWrapperLogger" batchSize="1000">
            <target xsi:type="Database"
                    name="Database"
                    connectionStringName="ApplicationServices">
                <commandText>
                    insert into Logs(UserId, Level, Message, Exception, StackTrace, DateCreated) values(CASE WHEN @UserId IS NOT NULL AND @UserId = CAST('00000000-0000-0000-0000-000000000000' AS uniqueidentifier) THEN @UserId ELSE @UserId END, @Level, @Message, @Exception, @StackTrace, @DateCreated);
                </commandText>
                <parameter name="@UserId" layout="${event-context:item=${guid:format=UserId}}"/>
                <parameter name="@Level" layout="${level}"/>
                <parameter name="@Message" layout="${message}"/>
                <parameter name="@Exception" layout="${exception}"/>
                <parameter name="@StackTrace" layout="${stacktrace}"/>
                <parameter name="@DateCreated" layout="${date}"/>
            </target>
        </target>
    </targets>

    <rules>
        <logger name="*" writeTo="Database"/>
    </rules>
</nlog>

public static void Write(string message, Exception ex)
        {
            var logEventInfo = new LogEventInfo(NLog.LogLevel.Error, "", message);

            if (HttpContext.Current != null)
            {
                logEventInfo.Properties.Add("UserId", Guid.NewGuid());
            }

            logEventInfo.Exception = ex;

            logger.Log(logEventInfo);
        }

I am getting the error

Exception occurred in NLog ---> System.Data.SqlClient.SqlException: Conversion failed when converting from a character string to uniqueidentifier.

when I try to insert a NULL in the UserId column of the table which can take NULLS.

<?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" internalLogFile="C:\Development\Projects\Pearson\ARIES\Development\Websites\ARIES.Web\NLog.log" internalLogLevel="Debug" >

    <!-- make sure to set 'Copy To Output Directory' option for this file -->
    <!-- go to http://nlog-project.org/wiki/Configuration_file for more information -->

    <targets>
        <target xsi:type="AsyncWrapper" queueLimit="2000" overflowAction="Grow" name="AsyncWrapperLogger" batchSize="1000">
            <target xsi:type="Database"
                    name="Database"
                    connectionStringName="ApplicationServices">
                <commandText>
                    insert into Logs(UserId, Level, Message, Exception, StackTrace, DateCreated) values(CASE WHEN @UserId IS NOT NULL AND @UserId = CAST('00000000-0000-0000-0000-000000000000' AS uniqueidentifier) THEN @UserId ELSE @UserId END, @Level, @Message, @Exception, @StackTrace, @DateCreated);
                </commandText>
                <parameter name="@UserId" layout="${event-context:item=${guid:format=UserId}}"/>
                <parameter name="@Level" layout="${level}"/>
                <parameter name="@Message" layout="${message}"/>
                <parameter name="@Exception" layout="${exception}"/>
                <parameter name="@StackTrace" layout="${stacktrace}"/>
                <parameter name="@DateCreated" layout="${date}"/>
            </target>
        </target>
    </targets>

    <rules>
        <logger name="*" writeTo="Database"/>
    </rules>
</nlog>

public static void Write(string message, Exception ex)
        {
            var logEventInfo = new LogEventInfo(NLog.LogLevel.Error, "", message);

            if (HttpContext.Current != null)
            {
                logEventInfo.Properties.Add("UserId", Guid.NewGuid());
            }

            logEventInfo.Exception = ex;

            logger.Log(logEventInfo);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-12-04 19:27:11

使用 SQL Profiler 修复了该问题。它正在为 null GUID 发送一个空字符串。

insert into Logs(UserId, Level, Message, Exception, StackTrace, DateCreated) values(CASE WHEN @UserId = '' THEN NULL ELSE convert(uniqueidentifier, @UserId) END, @Level, @Message, @Exception, @StackTrace, @DateCreated);

Fixed it by using SQL Profiler. It was sending an empty string for the null GUID.

insert into Logs(UserId, Level, Message, Exception, StackTrace, DateCreated) values(CASE WHEN @UserId = '' THEN NULL ELSE convert(uniqueidentifier, @UserId) END, @Level, @Message, @Exception, @StackTrace, @DateCreated);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文