SQL Persisted Computed Column ANSI_NULLS

I had to recreate a table in SQL Server just because it was created with ANSI_NULLS ON

Here you can check the requirements to create a persisted  computed column on SQL Server.

“…the SET options in the following table must be set to the values shown in the Required value column…”

SET options Required value Default server value Default

OLE DB and ODBC value

Default

DB-Library value

ANSI_NULLS ON OFF ON OFF
ANSI_PADDING ON ON ON OFF
ANSI_WARNINGS* ON OFF ON OFF
ARITHABORT ON ON OFF OFF
CONCAT_NULL_YIELDS_NULL ON OFF ON OFF
NUMERIC_ROUNDABORT OFF OFF OFF OFF
QUOTED_IDENTIFIER ON OFF ON OFF

 

Add WCF Message Id in every log4net message

I needed to have the WCF message identifier in every log message because I needed to correlate the log messages from a particular WCF call.

The best way to achieve this with log4net is to use context properties.

Since I’m already using Unity and Interception on my WCF Services, I’ve decided to implement a custom Call Hander


namespace CustomCallHandlers
{
 using System;
 using Microsoft.Practices.Unity.InterceptionExtension;

 /// <summary>
 /// Call Handler to setup wcf message id in log4net context
 /// </summary>
 public class ServiceCallHandler : ICallHandler
 {
 /// <summary>
 /// The message identifier
 /// </summary>
 private const string MessageIdKey = "MessageId";

 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceCallHandler"/> class.
 /// </summary>
 /// <param name="order">The call handler's execution order.</param>
 public ServiceCallHandler(int order)
 {
  this.Order = order;
 }

 /// <summary>
 /// Gets or sets the order.
 /// </summary>
 public int Order { get; set; }

 /// <summary>
 /// Invokes the specified input.
 /// </summary>
 /// <param name="input">The input.</param>
 /// <param name="getNext">The get next.</param>
 /// <returns>The method return for the nexte invocation</returns>
 public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
 {
  if ((System.ServiceModel.OperationContext.Current != null) &&
      (System.ServiceModel.OperationContext.Current.IncomingMessageHeaders != null))
  {
   if (System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.MessageId != null)
   {
    string messageId = System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.MessageId.ToString();
    log4net.ThreadContext.Properties[MessageIdKey] = messageId == "(null)" ? Guid.NewGuid().ToString() : messageId;
   }
  }

  IMethodReturn resultado = getNext()(input, getNext);

  // Cleaning
  log4net.ThreadContext.Properties[MessageIdKey] = null;

  return resultado;
  }
 }
}

If you want to see how to configure log4net appenders to write custom properties check this post.

 

 

Custom properties log4net

I had the need to add some custom properties in my log4net messages.

I used the log4net contexts like this:


log4net.ThreadContext.Properties[MessageIdKey] = messageId;

And then updated my rolling file appender:


<!-- Rolling File Appender -->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MM-yyyy HH:mm:ss,fff} (%logger) [%2thread]     [%property{MessageId}] %5level %message%newline" />
</layout>
</appender>

and my AdoNetAppender:


<!-- ADO Appender -->
 <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
 <bufferSize value="5" />
 <lossy value="false" />
 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 <connectionString value="data source=localhost;initial catalog=MyDb;integrated security=true;" />
 <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MessageId]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @messageId)" />

 ......

 <parameterName value="@messageId" />
 <dbType value="String" />
 <size value="255" />
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%property{MessageId}" />
 </layout>
 </parameter>

 </appender>