<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aidan O&#39;leary on Medium]]></title>
        <description><![CDATA[Stories by Aidan O&#39;leary on Medium]]></description>
        <link>https://medium.com/@aidanoleary?source=rss-7a5855f22217------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sYGQXsdU3cFpc5lv.</url>
            <title>Stories by Aidan O&amp;#39;leary on Medium</title>
            <link>https://medium.com/@aidanoleary?source=rss-7a5855f22217------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:19:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aidanoleary/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adding Logging without cluttering existing code]]></title>
            <link>https://medium.com/capgemini-microsoft-team/adding-logging-without-cluttering-existing-code-745c9b1c616?source=rss-7a5855f22217------2</link>
            <guid isPermaLink="false">https://medium.com/p/745c9b1c616</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Aidan O'leary]]></dc:creator>
            <pubDate>Wed, 29 Apr 2020 12:13:05 GMT</pubDate>
            <atom:updated>2020-04-29T12:13:05.930Z</atom:updated>
            <content:encoded><![CDATA[<h3>Adding logging without cluttering existing code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-9t6hXMs3GXaA-4kBjHWRA.jpeg" /></figure><p>During software development, it is often required to add logging capability to specific objects. However, in order to support the future maintainability of the solution you may want to keep this logging functionality separate from the existing code. This post presents an approach which we have successfully deployed in the past to decorate existing code artifacts with logging capabilities. The examples will be shown in C# but the concepts can be applied to any programming language.</p><p>To further understand the focus of this post, let’s examine some ways of separating the logging from existing code. In the example below, logging is included in a class which performs some business logic. In some scenarios this is okay.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ccb513f366553b39d1a71201fb4a148/href">https://medium.com/media/6ccb513f366553b39d1a71201fb4a148/href</a></iframe><p>In order to improve upon the above example, we can use <a href="https://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a> and hide the logger behind an interface. This makes it easier to switch out implementations of the logger and test. The downside, however, is that the logging implementation, is included in the code which contains your logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68c098495e6721ea01b1fdb7dac83ea0/href">https://medium.com/media/68c098495e6721ea01b1fdb7dac83ea0/href</a></iframe><p>Alternative approaches for separating logging from the core business logic are as follows.</p><p>One approach is to create a new class that implements the same interface as the object you wish to log and then wrapping it using the <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator Design Pattern</a>.</p><p>The constructor of your new class will then take an instance of the previous class a parameter. Each method that you then want to perform logging on will perform it’s logging in the logger decorator and subsequently invoke the same method of the decorated class to perform the existing functionality.</p><p>An implementation will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f152e5f9080bf5521e0d51e91ea958d/href">https://medium.com/media/2f152e5f9080bf5521e0d51e91ea958d/href</a></iframe><p>Another approach, involves sub-classing the class you wish to perform logging on. Logging is then performed in the methods of the subclass and the methods of the super class are called to continue the normal execution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ac46c545752c799c66c66fb7904e670/href">https://medium.com/media/8ac46c545752c799c66c66fb7904e670/href</a></iframe><p>Both methods can be supplemented using the Factory pattern to allow quick switching of the classes with logging and the classes without logging. For example, if logging needs to be determined at run time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f56c0c36afbf3da81cac664b3584961/href">https://medium.com/media/4f56c0c36afbf3da81cac664b3584961/href</a></iframe><p>To put it all together you can get an object with or without logging by using the relevant factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3972ae607960ac7bdeb978449c9eb5a8/href">https://medium.com/media/3972ae607960ac7bdeb978449c9eb5a8/href</a></iframe><p>The final method involves using a <strong>Publish/Subscribe</strong> event base model that notifies a logger when a specified method has been called. This involves attaching a logger to the on-change method of the object you wish to log. This is demonstrated in the following code snippets.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d40a73ba97ccbdd3ffe9147a649731e/href">https://medium.com/media/1d40a73ba97ccbdd3ffe9147a649731e/href</a></iframe><p>All three of these methods allow you to abide by the <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle"><strong>open/close </strong>principle</a> while adding logging functionality. As a result of adopting this principle, the likelihood of errors occurring within our solution is reduced while enhancing the test-ability of the code base.</p><p>In summary, while these logging approaches may not be applicable to all scenarios, when leveraged, they have the potential deliver robust maintainable solutions. Hopefully you have enjoyed this post and the next time you are required to add logging to an application try to consider whether this can be done without significant changes to your existing code.</p><p>Take a look here at our open roles and <a href="https://www.capgemini.com/gb-en/careers/job-search/?search_term=Microsoft&amp;show_posts=50&amp;utm_source=medium_blog&amp;utm_medium=referral&amp;utm_content=countryorganic_link_blog_medium_blog&amp;utm_campaign=other">Join The Microsoft Team</a> at Capgemini.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=745c9b1c616" width="1" height="1" alt=""><hr><p><a href="https://medium.com/capgemini-microsoft-team/adding-logging-without-cluttering-existing-code-745c9b1c616">Adding Logging without cluttering existing code</a> was originally published in <a href="https://medium.com/capgemini-microsoft-team">Capgemini Microsoft Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>