How to turn up logging for CM

Aug 5, 2010 at 6:47 PM
Hi In my sample app, I have an issue where the event is not getting fired. Obviously, it is my setup problem and I wanted to tun up the logging in Caliburn.Micro and write it to the console. Hence I created a simple ConsoleLogger. I am probably missing something very obvious because I do not see a way to initialize the LogManager. How can I set it up such that I can get the logging turned up for CM? Thanks rockmeister
Aug 5, 2010 at 7:36 PM
Edited Aug 5, 2010 at 7:42 PM

It should be something like:

Logging.GetLog = type => new ConsoleLogger(type);

You have to run this line early in the app lifecycle (application contructor or Bootstrapper.Configure override should be good candidates).

Aug 5, 2010 at 8:04 PM
I am going to blog something about this in the next week, but here is the basics: internal class DebugLogger : Caliburn.Micro.ILog { #region Implementation of ILog public void Info(string format, params object[] args) { System.Diagnostics.Debug.Write("[" + DateTime.Now.ToString("o") + "] ", "INFO"); System.Diagnostics.Debug.WriteLine(format, args); } public void Warn(string format, params object[] args) { System.Diagnostics.Debug.Write("[" + DateTime.Now.ToString("o") + "] ", "WARN"); System.Diagnostics.Debug.WriteLine(format, args); } public void Error(Exception exception) { System.Diagnostics.Debug.Write("[" + DateTime.Now.ToString("o") + "] ", "ERROR"); System.Diagnostics.Debug.WriteLine(exception.Dump()); } #endregion } and then in a static constructor for your bootstrapper class add: static MyBootStrapper() { LogManager.GetLog = type => new DebugLogger(); } Hope that helps.
Aug 5, 2010 at 11:39 PM

Hi marcoamendola/dbuksbaum
Thanks a bunch that did help me in getting the log onto the console. 

Off Topic
I do want to get this off my chest...
Disclaimer: If it is not already obvious, I pretty new to C# development.  I come from a C, C++ from long time back, went into management (i.e. no programming) for over a decade. I have started using C# recently (last week!) and want to get callus back on my fingertips :-)

Now to my real beef.  I had to go through multiple gyrations to see logs on the console.
(1) First I had trouble on how to initialize the LogManager (I guess I need to understand what Logging.GetLog = type => new ConsoleLogger(type); means (some kind of func-ptr assignment I suppose)
(2) Still the console output did not show up until I changed the Application to be a Console Application.


Aug 6, 2010 at 8:21 AM

1) Exactly. The  "parameter => statement" is a "lambda expression", one of the syntax available to create a delegate (sort of function pointer, but memory safe)

2) For debugging purposes, you should use the Debug class, as dbuksbaum pointed out: logging messages will be printed in the debug window when running in VS.

Aug 6, 2010 at 3:36 PM

A fellow C/C++ guy. I can sympathize :)

As Marco said, the GetLog assignment is a lamba expression. It is similar (though not quite the same) as a function pointer, with the added feature of being anonymous in this case. Here is same assignment a little more flushed out, if it helps:

public ILog CreateLog(Type type) { return new ConsoleLogger(type); }

Logging.GetLog = CreateLog;

As Marco said in point 2, use the debug class when running in the debugger. Your life will be better :).  As an alternative, you can hook up logging to Log4Net or NLog or other logging libraries that allow you to redirect logging to many sources, including files, databases, networks, consoles, and/or the debugger. When I finish my blog post on logging for Caliburn.Micro this week, I will cover some of that.