31 July 2008

Event Logging

Here's a quick method to log exceptions to the event log:
private void LogException(Exception exception){
EventLogPermission permissions = new EventLogPermission(EventLogPermissionAccess.Administer, ".");

using (EventLog log = new EventLog("Application")){
permissions.PermitOnly();

if (!EventLog.SourceExists("MySourceName")){
EventLog.CreateEventSource("MySourceName", "Application");
}

log.Source = "MySourceName";
log.WriteEntry(exception.ToString(), EventLogEntryType.Error);
}
}
A couple of things to note:
  • The using statement makes sure the EventLog object is disposed without fail
  • The EventLogPermission permits this callee to create an event source if required and to write to the event log

16 July 2008

Using Static Alias

C# developers will be familiar with this syntax:

this.DoSomething();

whereby DoSomething() is some other method and qualifying it with the
this keyword is good practice. But how about static methods? You'll have to qualify it with the class name:

MyClass.DoSomeStaticThing();

I've never felt right qualifying static methods and properties within the class itself (it is of course required when calling from outside the class) and it gets a bit silly for really long class names, eg:

ServiceModelConfigurationSectionGroupCollection.DoSomeStaticThing();

That might be the worst case scenario class name, but when it is used more than once, the code starts looking littered:

MyVeryLongClassName.TryEnterReadLock(MyVeryLongClassName.Timeout, MyVeryLongClassName.IsReadOperation);

One option is to not qualify at all:

DoSomeStaticThing();

but this just doesn't promote good code clarity, especially when there are numerous static methods and/or properties that seem to float around with no qualifiers at all.

What I've started doing is using an alias called "This" which is just a shortcut to my class:

namespace MyNamespace{
using This = MyNamespace.MyVeryLongClassName;
public class MyVeryLongClassName{ ...

Now I can succinctly qualify static methods and properties:

This.DoSomeStaticThing();


There is an analogous distinction (albeit subtle) between
this (instance) and This (static) which is consistent with .NET naming conventions between class types (Pascal case) and instances (Camel case).

08 July 2008

Using ReaderWriterLockSlim

I was revisiting some code I needed to rewrite and realised I really needed to implement a locking mechanism. I remembered that of all the techniques available in the .NET framework, the System.Threading.ReaderWriterLock was going to suit the most because I needed to allow multiple concurrent reads but only one write at any time. When browsing through the class reference, I noticed a new class called ReaderWriterLockSlim and decided to have a read about it.
According to MSDN, the ReaderWriterLockSlim is a much more performant version of ReaderWriterLock and recommends developers to ditch the older class in favour of the newer one.
One tip I have is to use the TryEnterReadLock/TryEnterWriteLock methods instead of the EnterReadLock/EnterWriteLock methods. The reason is that the latter methods may end up blocking indefinitely while the former methods give you a timeout value:

lock = new ReaderWriterLockSlim();
...
if (lock.TryEnterWriteLock(1000)){
try{
//do some write operation that requires locking
}
catch(Exception e){
//handle the exception
}
finally{
lock.ExitWriteLock();
}
}
else{
//optionally throw a System.TimeoutException
}


You need to be diligent in ensuring all write operations that require locking are actually enclosed by write locks or you defeat the purpose of having a locking mechanism. The lock variable will also need to be a singleton so that locks are honoured across all threads.