<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Unity Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/category/dotnet/unity/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/category/dotnet/unity/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 12:46:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Add WCF Message Id in every log4net message</title>
		<link>https://blogit.create.pt/ricardocosta/2015/11/24/add-wcf-message-id-in-every-log4net-message/</link>
					<comments>https://blogit.create.pt/ricardocosta/2015/11/24/add-wcf-message-id-in-every-log4net-message/#respond</comments>
		
		<dc:creator><![CDATA[Ricardo Costa]]></dc:creator>
		<pubDate>Tue, 24 Nov 2015 20:14:06 +0000</pubDate>
				<category><![CDATA[log4net]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Unity]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/ricardocosta/?p=871</guid>

					<description><![CDATA[<p>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&#8217;m already using Unity and Interception on my WCF Services, I&#8217;ve decided to implement a custom Call [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2015/11/24/add-wcf-message-id-in-every-log4net-message/">Add WCF Message Id in every log4net message</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>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.</p>
<p>The best way to achieve this with <a title="log4net" href="https://logging.apache.org/log4net/" target="_blank">log4net</a> is to use <a title="log4net context properties" href="https://logging.apache.org/log4net/release/manual/contexts.html" target="_blank">context properties</a>.</p>
<p>Since I&#8217;m already using <a title="Unity" href="https://github.com/unitycontainer/unity" target="_blank">Unity</a> and Interception on my WCF Services, I&#8217;ve decided to implement a custom <a title="Call Handler" href="https://msdn.microsoft.com/en-us/library/ff660871(v=pandp.20).aspx" target="_blank">Call Hander</a></p>
<pre class="brush: csharp; title: ; notranslate">

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

 /// &lt;summary&gt;
 /// Call Handler to setup wcf message id in log4net context
 /// &lt;/summary&gt;
 public class ServiceCallHandler : ICallHandler
 {
 /// &lt;summary&gt;
 /// The message identifier
 /// &lt;/summary&gt;
 private const string MessageIdKey = &quot;MessageId&quot;;

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

 /// &lt;summary&gt;
 /// Gets or sets the order.
 /// &lt;/summary&gt;
 public int Order { get; set; }

 /// &lt;summary&gt;
 /// Invokes the specified input.
 /// &lt;/summary&gt;
 /// &lt;param name=&quot;input&quot;&gt;The input.&lt;/param&gt;
 /// &lt;param name=&quot;getNext&quot;&gt;The get next.&lt;/param&gt;
 /// &lt;returns&gt;The method return for the nexte invocation&lt;/returns&gt;
 public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
 {
  if ((System.ServiceModel.OperationContext.Current != null) &amp;&amp;
      (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&#x5B;MessageIdKey] = messageId == &quot;(null)&quot; ? Guid.NewGuid().ToString() : messageId;
   }
  }

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

  // Cleaning
  log4net.ThreadContext.Properties&#x5B;MessageIdKey] = null;

  return resultado;
  }
 }
}

</pre>
<p>If you want to see how to configure log4net appenders to write custom properties check this <a title="Custom properties log4net" href="http://blogit.create.pt/ricardocosta/2015/11/23/custom-properties-log4net/" target="_blank">post</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2015/11/24/add-wcf-message-id-in-every-log4net-message/">Add WCF Message Id in every log4net message</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/ricardocosta/2015/11/24/add-wcf-message-id-in-every-log4net-message/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
