December 21, 2014

Weak References with Compiled Transforms

There is a good description of this generic WeakReference<> class here.
Here is an example of using WeakReferences for caching XslCompiledTransform's:
private static Dictionary<string, WeakReference<XslCompiledTransform>> xsltLookupTable = 
  new Dictionary<string, WeakReference<XslCompiledTransform>>();

public XslCompiledTransform GetCompiledTransform(string xslFileName)
{
    XslCompiledTransform xct = null;
    bool found = xsltLookupTable.ContainsKey(xslFileName);
    if (found) // IF the transform is already cached
    {   // Try and get it
        WeakReference<XslCompiledTransform> xctWr = xsltLookupTable[xslFileName] 
           as WeakReference<XslCompiledTransform>;
        xctWr.TryGetTarget(out xct); // Try and get it from the WeakReference
        m_logger.WriteTrace("Found XslCompiledTransform entry for \'" + 
           xslFileName + "\' in the cache");
        // Note the entry maybe null (if the weak reference expired)
    }
        
    if (xct == null) // IF the compiled transform was not already cached
    {
        // Create it
        xct = new XslCompiledTransform();
        xct.Load(xslFileName, 
          new XsltSettings { EnableDocumentFunction = true }, 
          new XmlUrlResolver());
        // Insert it into a WeakReference
        WeakReference<XslCompiledTransform> wr = new 
          WeakReference<XslCompiledTransform>(xct);
        if (found)
        {
          m_logger.WriteTrace("Removing XslCompiledTransform entry for \'" + 
              xslFileName + "\' as it was null in the cache");
          xsltLookupTable.Remove(xslFileName);
        }
        xsltLookupTable.Add(xslFileName, wr); // Add the WeakReference to the cache
        m_logger.WriteTrace("Adding XslCompiledTransform entry for \'" + 
          xslFileName + "\'");
    }
    return xct;
}
It uses the new generic WeakReference<> class (available in .NET 4.5?). This class could be further refactored into a generic caching class if it was required.

December 12, 2014

Configuring log4net for specific classes or namespaces

Here is some sample xml that goes in the application config file.
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    ...
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%utcdate [%t] %-5p [] - %m%n"/>
      </layout>
    </appender>
    <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%utcdate [%t] %-5p %c [] - %m%n"/>
      </layout>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="FilePath.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="1000KB" />
      <maxSizeRollBackups value="20" />
      <param name="RollingStyle" value="Size" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%utcdate [%t] %-5p %c [] - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="INFO"/>
      <appender-ref ref="RollingFile"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="OutputDebugStringAppender"/>
    </root>
    <!--Level values are DEBUG, INFO, WARN, ERROR -->
    <!--Entries for "top level" server module classes, to allow tracing of method calls -->
    <logger name="Some.Name.Space.ClassA.">
      <level value="INFO"/>
    </logger>
    ...
    <!--Entries for namespaces, to allow full tracing inside modules -->
    <logger name="Some.Particular.Namespace">
      <level value="INFO"/>
    </logger>
    ...
    <!--Entries for certain individual classes -->
    <logger name="Some.Deep.Level.NameSpace.SpecificClass.">
      <level value="DEBUG"/>
    </logger>
    ...
  </log4net>
...  
</configuration>

December 5, 2014

CallerMemberNameAttribute in .NET 4.5

Use of "CallerMemberNameAttribute" for an IPropertyNotifyChanged base class implementation
More declaration info can be found here

I found that this did not work in VS 2010 but did work in VS 2013 as it is compiler dependent. You need to install KB2468871 on top of .net 4 framework to be able to use. Alternatively, you can simply define the attribute yourself to get it to work.
namespace System.Runtime.CompilerServices
{
  /// 
  /// Allows you to obtain the method or property name of the caller.
  /// 
  [AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
  public sealed class CallerMemberNameAttribute : Attribute { }
}
Here is an example of how to use it to make a simple logger (only 1 method shown):
class SimpleLogger 
{
...
 public void Error(
   [CallerMemberName] string memberName = "",
   [CallerFilePath] string sourceFilePath = "",
   [CallerLineNumber] int sourceLineNumber = 0,
   string message)
 {
    Debug.Write(" Method/Property=" + memberName);
    Debug.Write(" SourceFilePath=" + sourceFilePath);
    Debug.Write(" SourceLineNumber=" + sourceLineNumber);
    Debug.WriteLine(" - " + message);

 }
...
}
and invoking it:
SimpleLogger logger = new SimpleLogger();
...
catch(Exception ex)
{
  logger.Trace(" Exception caught " + ex.ToString()); // The attributed parameters are inserted by the compiler at compile time
}