Simplest way to logging

I use NLog for logging. I used to keep Logger property in every class I needed to log from and then have Winsdor container inject it with appropriate logger.

All of that changed when I switched to AutoFac and I wanted to simplify working with logger, especially since not all classes I want to log from are managed by the DI container.

The best solution I came up with is to use extension methods in .NET 3.5. Here's the code:

using System; 
using NLog; 
namespace Logging 
{ 
    public interface INeedToLog 
    { 
    } 


    public static class LogExtensions 
    { 
        public static Logger Log( this INeedToLog needToLogObj ) 
        { 
            var type = needToLogObj.GetType(); 
            return Logging.GetLogger( type ); 
        } 
    } 


    public static class Logging 
    { 
        public static Logger GetLogger( Type typeToLogFrom ) 
        { 
            return LogManager.GetLogger( typeToLogFrom.Name ); 
        } 


        public static Logger GetLogger( object objectToLogFrom ) 
        { 
            var typeToLogFrom = objectToLogFrom.GetType(); 
            return LogManager.GetLogger( typeToLogFrom.Name ); 
        } 
    } 
} 

I simply inherit from INeedToLog from any class in which I need to log. Then extension method Log becomes available and I can do something like this.

this.Log().Info( "Info text" );

This method for logging might be intrusive into interface hierarchy, but it's very simple to set up, even if you don't use DI. I like that!



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 7/28/2008 at 11:43 AM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Drip 0.3

Drip 0.3 has been released. It's a memory leak detector for IE. Very useful for a web developer who's creating rich client applications. I have been using it for some time and it has helped me a lot on several occasions.

New release brings some serious improvements from the previous version. Some bugs and problems have been fixed. What's more important is that Drip 0.3 in addition to showing DOM objects (that cause the leaks) that are not released from memory it now provides an option to show the values of their fields. For each DOM object you can open a properties window. In it you can find a lot of interesting information, possibly pointing you to what has caused the leak. The tool is really great and with every release it's getting better.

This release got me thinking, again. IE has been around for a long time (with all memory leak problems), but nobody was interested enough in developing such tool until recently. Appearance of Drip is just another sign that web development is on the rise. I wonder why? Is it because of Google and renewed interest it has created in remote access applications (XMLHttp. Again, technology for these applications has been around for a while, but became popular only recently). Or is it just another swing of the pendulum getting more people interested in web development again? Is it because mobile devices with wireless Internet access are becoming more and more popular? Or is there something else? (I'm sure there is) It's probably a combination of all these and other factors. I wonder, though, what awaits us in the future. Any thoughts?



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 11/29/2005 at 1:44 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Free component offer

I read many blogs of fellow .NET developers and recently this page came to my attention (through their blog). It's an offer of a free DotNetMagic developer's license in exchange for a blog post covering new .NET UI library released by Component Factory.

Component Factory is a new name in the field of .NET UI controls development. Their first release, Krypton Toolkit, is a collection of UI WinForms controls for .NET 2 and Visual Studio 2005. It's free for commercial use and so I think quite a few people would be interested in checking it out. Controls are actually quite good (considering they are free) and allow developers to reproduce MS Office look (mostly Outlook with it's paneled layout) in the applications. Controls also support visual customization.

You can download controls by navigating to Component Factory downloads page. They do ask you to submit your e-mail and you will be automatically subscribed to the e-mail newsletter (you can opt out at any moment).

I have being considering if I should write a post about this or not. On one hand I felt that this would be an advertisement, and people usually don't like reading advertisements. On the other hand the component is free and in all likelihood I could have talked about it sooner or later. This way I get a nice bonus and I will be able to improve the look of .NET applications that I work on, some of which I plan to release... So why not?:)



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 11/22/2005 at 2:00 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

nAnt time stamp, Part II

Not long ago I talked about nAnt time stamping. It was an interesting, but unnecessary exercise. If I would have been a smart and read the whole manual I would have discovered nAnt tstamp task. It's a task to generate time stamp by template.

The new code that uses this task to time stamp projects is much shorter:

<tstamp />
 <zip zipfile="${release.dir}\${project.name}.${tstamp.date}.zip">
  <fileset basedir="${working-release.dir}">
   <include name="**" />
  </fileset>
 </zip>

tstamp by default sets tstamp.date to yyyyMMdd. This is just what I needed and the code is much simpler.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 11/20/2005 at 3:10 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Automatic Monorail helper docs

I have mentioned before that I work on Monorail documentation. Mostly I'm doing this in my spare time. I want to make sure I have correct and valid documentation. I can use reflection to call all methods (with some predefined parameters). My code parses their output and generates some (format and small example. After this I still need to add explanations manually) of the documentation automatically. I got it done and now I want to release this small project. You can get it here (VS2003 C# project).

The program can be used to make it convenient to see how to use different helpers (certain Monoral components) as it generates example input and output, especially if there're no docs available for the helper yet. You can also use this if you are developing or modifying some helper and want to see that it works as you expect it to work.

HelperDocumentingComponent is used to generate documentation. It uses reflection to call all of the methods declared in the current helper only (No overridden methods are called). Output from each method is then converted to string (ToString) and shown on the webpage. CreateParam method generates a parameter for a method from a template picked by the parameter type.

Generated docs follow this format:

/// Docs in XML format to be inserted to the code
/// ...
/// ...
Example output as it would be added to the page

It is possible to specify which helper to document. Homepage (default.aspx) is using it to create links to all existing helpers. To show docs for a specific helper navigate to "~/helperdocs/generatehelperdocs.ashx?helperName=helpername". By default the program tries to find the helper in the Monorail assembly, but you can easily modify the source to look for it in a different assembly (or several others for that matter).

If a method to be documented returns some object of a class the name of the class is shown instead of the object's value. In the future I might add code to show all of the object's properties.

I am using several tools with this project. nAnt to run build files and Cassini to show example website without having to add it to IIS. I don't use nAnt to compile the solution, only to generate distribution. Building from VS.NET is easier for me. If you want to use nAnt to compile just launch "go.cmd". I believe it should work fine.

To launch the server start "runServer.cmd". When Cassini dialog opens click on the link.

I have not included nAnt to keep release archive file size small. If you choose to get it place it in "nAnt" folder in the "tools" directory. You can find nAnt here. Full Cassini distribution can be found here.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 10/28/2005 at 1:47 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Timestamping in nAnt

I'm using nAnt to automate my projects' build and release. That means from time to time I will write about my experience with automatic building and releasing, and interesting things I find in the process.

Today I had to solve a small problem in a nAnt scripts. I needed to timestamp archive of the release. While nAnt zip task can timestamp the file, the format it uses (MM/DD/YYYY HH:MM:SS) is not suitable for me. I prefer for the project release date to be a part of the version and to follow minor version (i.e. 0.5.20051004). I'm sure there are arguments for and against using such version format, but I like this and I wanted to solve specific problem of generating it.

This is what I came up with:

<property name="date.now" value="${ datetime::now() }"/>
<property name="year.now" value="${ datetime::get-year( date.now )}"/>
<property name="month.now" value="${ datetime::get-month( date.now )}"/>
<property name="month.now" value="0${ month.now }" if="${ int::parse( month.now ) &lt; 10 }"/>
<property name="day.now" value="${ datetime::get-day( date.now )}"/>
<property name="day.now" value="0${ day.now }" if="${ int::parse( day.now ) &lt; 10 }"/>
<property name="timestamp" value="${year.now}${month.now}${day.now}"/>

timestamp property will have current date in YYYYMMDD format. It can be used to timestamp the archive (what I'm using it for), to generate build number, etc.

This is an example of how you could use it to timestamp zip archive:

<zip zipfile="${release.dir}\${project.name}.${timestamp}.zip">
 <fileset basedir="${working-release.dir}">
  <include name="**" />
 </fileset>
</zip>

Some of the properties, like release.dir must be initialized earlier, but I wanted to keep example simple so I omitted all initialization.

If you find a simpler way to generate timestamp let me know. I'd be delighted to hear about it.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 10/4/2005 at 12:39 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Generating Castle documentation with nAnt

I got to work a little bit on the Castle and I decided to concentrate on writing documentation. The Castle is really a good project and it’s a shame it's unpopular because it’s documentation is lacking. It’s design is so intuitive that in most cases once you understand the idea you won’t have to go back to documentation often.

nAnt is used by the Castle developers to build it. So I started off by adding tasks to generate documentation for all of the Castle sub-projects. The end result I wanted was a single CHM file with documentation for all assemblies. Simple, I thought, but I found out it’s harder than it looks. This brings me to the today’s post topic (finally, after 2 paragraphs, but I feel it’s appropriate to supply some background).

nAnt has a special task (task is one step in a build process) which uses nDoc to generate documentation from XML comments.

I thought I could just configure the task to iterate through all “bin” directories (all projects build into “bin” sub-folder of project’s folder). Iteration works OK, but it goes through all folders (those matching and not matching the pattern). In addition it also goes through all “.svn” folders (used by Subversion for source control) which practically doubles the number of folders nAnt traverses. According to documentation nAnt should've excluded “.svn” folders from traversal, but in this case it didn’t… I’m not sure why.

Another problem is that I only want to document actual release assemblies and ignore test assemblies (with “Tests” somewhere in the assembly name). nAnt decided to ignore my exclude pattern and just included everything.

The solution I came up with is to copy all assemblies to be documented to a separate folder. I hard-coded project folder names (yes, hard-coding is bad, but I could not find a way around it) and the process of selecting assemblies is very quick now. It still takes around 5 minutes on my computer to generate all documentation. There are more than 8000 class members to create HTML pages for, but it’s much faster than before.

Next phase is to add members’ documentation and examples to the code. I’m planning to start with Monorail and move on slowly. Mostly I aim for quality and not quantity. I don't want to go back often to redo it.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 8/3/2005 at 1:01 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

RemoteContent Helper for Monorail (updated)

The RemoteContent helper I released on Monday had a flaw. It pulls and displays the feed correctly, but the feed by default has escape characters (HTML-encoded). This resulted in any HTML in the feed to be shown as HTML source code and not as an actual web page.

I fixed the problem by utilizing System.Web.HttpUtility.HtmlDecode method. After the transformation takes place it's is decoded.

By default the feed will get decoded, but you can always disable decoding by calling $RemoteContentHelper.TransformFeed( "feedUrl", "transformation.xslt", false )

Get the new version of RemoteContentHelper here.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 7/16/2005 at 12:20 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

RemoteContent helper for Monorail

I’m finishing up the website for my JavaScript UI project and the only thing left is to add some content and fix the style a little bit. I’m also enjoying flexibility that Monorail provides. For example, I decided to publish announcements on the front page and to pull them from CS forums. CommunityServer is a separate application so it’s hard to access it directly to retrieve the posts, but forums expose an RSS feed with all the latest messages. With some inspiration from the CS community and Cuyahoga (which provides remote content module) I decided to pull announcements via RSS and transform them into HTML using XSLT.

Surprisingly it was easy to write the helper since .NET XML classes already support working with URLs and loading XML documents from remote locations. All I had to do was to write code to load XSLT from the local directory and spit out the transformation results. For those interested to see implementation I’ve uploaded the files together with an XSLT file to transform CS forum feed into HTML - RemoteContentHelper.zip

If you’re developing with Monorail and nVelocity adding the feed is really simple. Just add the following line to your template where you want the feed to appear:

$RemoteContentHelper.TransformFeed( "feedUrl", "transformation.xslt" )

Please feel free to leave a comment if you have any questions or remarks.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 7/11/2005 at 11:25 AM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Inversion of Control features

I was reading Castle IoC reference manual and realized something that I have not thought of before. By using IoC facilities it’s really easy to implement certain patterns, especially since Castle is really designed to be extensible. In reality, some of the patterns are already implemented and supported. For example by default all components in the IoC container are of the singleton lifestyle. The beauty of the framework is that it’s really easy to change the lifestyle of the object to, for example, transient (new component per request), without having to change the class code. Since components are singletons by default, reusability of the objects is encouraged. This should save some memory and execution time.

Another cool feature of the Castle is that it’s really simple to configure components. In the past, I might have had to either write my own class to extract configuration from the .config file or use some configuration framework, like Nini. Now my components will automatically initialize with the supplied configuration. I might still have to use Nini when a complex configuration will be required and application will need to save it (save user options for example). But the whole config deal is going to be really simplified.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Slava
Posted on: 7/10/2005 at 6:07 PM
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed