nLog 和 TransactionScope
我有一个 ASP.NET MVC 3(使用 Entity Framework 4.2)应用程序,它使用事务如下:
using (var transaction = new TransactionScope())
{
// Database action 1
// Database action 2
context.SaveChanges();
Logger.Info("Record X updated");
transaction.Complete();
}
我没有收到错误,但没有数据写入数据库。但是,日志文件目标(仅用于测试目的)工作正常。这是我的 nLog (我正在使用 v2)配置:
<?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"
internalLogToConsole="true"
internalLogToConsoleError="true" >
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}app_data\file.txt" />
<target xsi:type="Database" name="database">
<commandText>INSERT INTO [LogEntries] (TimeStamp, Message, Level, Logger) VALUES(GETDATE(), @msg, @level, @logger)</commandText>
<parameter name="@msg" layout="${message}" />
<parameter name="@level" layout="${level}" />
<parameter name="@logger" layout="${logger}" />
<dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
<connectionString>Data Source=${basedir}app_data\Logger.sdf</connectionString>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="database" />
</rules>
</nlog>
如果我将 Logger 行移到 transactionscope 之外,它就可以正常工作。所以我认为这与此有关。我使用 SQL Server 2008 r2 作为我的主数据库,使用 SQL Compact 4 作为我的日志数据库。另外,我尝试添加 useTranscations="true"。
有什么想法我做错了吗?
谢谢艾伦
I have an ASP.NET MVC 3 (using Entity Framework 4.2) application that uses transactions as follows:
using (var transaction = new TransactionScope())
{
// Database action 1
// Database action 2
context.SaveChanges();
Logger.Info("Record X updated");
transaction.Complete();
}
I get no errors, but no data is written to the database. However, the log file target (there just for testing purposes) works fine. Here is my nLog (I'm using v2) 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"
internalLogToConsole="true"
internalLogToConsoleError="true" >
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}app_data\file.txt" />
<target xsi:type="Database" name="database">
<commandText>INSERT INTO [LogEntries] (TimeStamp, Message, Level, Logger) VALUES(GETDATE(), @msg, @level, @logger)</commandText>
<parameter name="@msg" layout="${message}" />
<parameter name="@level" layout="${level}" />
<parameter name="@logger" layout="${logger}" />
<dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
<connectionString>Data Source=${basedir}app_data\Logger.sdf</connectionString>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="database" />
</rules>
</nlog>
If I move the Logger line outside of the transactionscope it works fine. So I think it's something to do with this. I'm using SQL Server 2008 r2 for my main database and SQL Compact 4 for my logging database. Also, I've tried adding useTranscations="true".
Any ideas what I'm doing wrong?
Thanks
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试让事务跨越两个不同的服务器,这是一个分布式事务。不幸的是,SQLCE不支持分布式事务(来自文档):
我很好奇为什么你不登录到SQL 2008 R2数据库,但是如果你必须登录到SQLCE,我想你必须跟踪事务是否提交,然后确定是否在
之后调用NLog TransactionScope
块完成。You are trying to have the transaction span two different servers, which is a distributed transaction. Unfortunately, SQLCE does not support distributed transactions (from the documentation):
I am curious why you don't log to the SQL 2008 R2 database, but if you must log to SQLCE, I think you'll have to track whether the transaction is committed and then determine whether to call NLog after the the
TransactionScope
block completes.