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
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
TransactionScopeblock completes.