Advertisement
JakeJBlues

One Extension Method for log4Net

May 5th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. // added the TActor  
  2.  public class Switch<TKey,TDecision,TActor,TParameter> {
  3.       protected static Func<TDecision, bool> Decision;
  4.       protected static Action<TActor, TParameter> OnTrue;
  5.       protected static Action<TActor, TParameter> OnFalse;
  6.  
  7.       public void Process(TDecision d,TActor o, Func<TParameter> param)
  8.       {
  9.          if (Decision(d))
  10.          {
  11.             OnTrue(o, param());
  12.          }
  13.          else
  14.          {
  15.             OnFalse(o, param());
  16.          }
  17.       }
  18.  
  19.       public void ProcessOnlyTrue(TDecision d, TActor o, Func<TParameter> param)
  20.       {
  21.          if (Decision(d))
  22.          {
  23.             OnTrue(o, param());
  24.          }
  25.       }
  26.  
  27.       public void ProcessOnlyFalse(TDecision d,TActor o, Func<TParameter> param)
  28.       {
  29.          if (!Decision(d))
  30.          {
  31.             OnFalse(o, param());
  32.          }
  33.       }
  34.  
  35.    }
  36.    
  37.    public class Switch<TKey, TDecision, TParameter> : Switch<TKey,TDecision,TDecision,TParameter> {            
  38.       public void Process(TDecision d,Func<TParameter> param ) {
  39.          Process(d,d,param);
  40.       }
  41.       public void ProcessOnlyTrue(TDecision d, Func<TParameter> param)
  42.       {
  43.          ProcessOnlyTrue(d, d, param);
  44.       }
  45.       public void ProcessOnlyFalse(TDecision d, Func<TParameter> param)
  46.       {
  47.          ProcessOnlyFalse(d, d, param);
  48.       }
  49.    }
  50.  
  51.    public class Debug : Switch<Debug, ILog, string> {
  52.       static Debug() {
  53.          Decision = log => log.IsDebugEnabled;
  54.          OnTrue = (log, msg) => log.Debug(msg);        
  55.       }
  56.    }
  57.  
  58.    public class Info : Switch<Info, ILog, string> {
  59.       static Info() {
  60.          Decision = log => log.IsInfoEnabled;
  61.          OnTrue = (log, msg) => log.Info(msg);        
  62.       }
  63.    }
  64.  
  65.    public static class ILogExtensions {
  66.       public static void Log<T>(this ILog log, Func<string> msg ) where T : Switch<T, ILog, string>,new() {
  67.          new T().ProcessOnlyTrue(log,msg);        
  68.       }
  69.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement