Skip to content
George Michaelides edited this page Apr 17, 2015 · 6 revisions

##Log Gem's events

Gem uses a logging facade to log various events. You can implement your own appender to intercept and format Gem's messages.

###IAppender The base interface is IAppender which has the following members

    void Info(string message);
    void Info(string message, params object[] args);

    void Debug(string message);
    void Debug(string message, params object[] args);

    void Warn(string message);
    void Warn(string message, params object[] args);

    void Error(string message);
    void Error(string message, params object[] args);

    void Fatal(string message);
    void Fatal(string message, params object[] args); 

Implement your own logging logic and add your IAppender instance to Gem.Auditor.RegisterAppender(IAppender appender) to intercept the logging events.

###ActionAppender

The ActionAppender implements IAppender and takes an Action and a format template as constructor arguments

    new ActionAppender(echo: Console.WriteLine,
                       formatTemplate: "[%Date{0:G}] %Verbosity - %Message");

Format Template options:

   %Verbosity - The output verbosity
   %Date{pattern} - The DateTime.Now %Date{0} for default
   %Message - The output message
   %NewLine - Environment.NewLine

You can add your own formatting rules by adding a new FormattingRule(Regex pattern, Func<string, string> replacementRule) to appender.AddFormatingRules(ActionAppender.FormattingRule[] rules)

 new FormattingRule(pattern: new Regex("%Date{.*}",RegexOptions.Compiled),
                    replacementRule: 
                    msg => String.Format(msg.Replace("%Date", ""), DateTime.Now)   

###ConfigurableAppender

Additionally you can use the ConfigurableAppender to invoke an action prior an event. The configurableAppender has the following options

   Action<TAppender> Info 
   Action<TAppender> Debug 
   Action<TAppender> Warn 
   Action<TAppender> Error 
   Action<TAppender> Fatal 

   ConfigurableAppender
   .Create(new ActionAppender(echo: Console.WriteLine),
           options =>
           {
               options.Info = appender => Console.BackgroundColor = ConsoleColor.Yellow
               options.Error = appender => Console.BackgroundColor = ConsoleColor.Red
           });

Clone this wiki locally