使用 NLOG 插入指南
出现错误。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 SQL Profiler 修复了该问题。它正在为 null GUID 发送一个空字符串。
Fixed it by using SQL Profiler. It was sending an empty string for the null GUID.