Send Log items through Mediator

Jul 5, 2010 at 6:34 PM
Is it possible or is it a good ide to use the Mediator to send log entries and then have handlers to catch them and log them??
Coordinator
Jul 5, 2010 at 8:53 PM
well you can do it .... not sure if it is a good idea... Maybe if you do a cutom version of the mediator... so use the same idea but make it custom for logging.... Its interesting idea but what should you gain? why not just having a single instance of a logger and just call Log method on that?
Jul 6, 2010 at 9:29 AM
Hi Marlon, I have a working solution for logging through a custom log mediator. I just have tested this with logging to Log4Net. I have a seperate Log4Net project ( this is exported and put in Extensions folder) that catches this mediator message and logs it through Log4Net ( configuration from app.config ) It works perfect but need to know if this is a way to go. I just see the easy of it to just send log messages through the Mediator. I just need to create a class that catch this LogItem like this ( this sample is for Log4net ): *********************************************************************************************** [ExportLogger(Name = "Log4Net")] public class Log4NetFramework : FormattableLoggerBase, ILoggerFramework { private readonly ILoggingMediator _loggingService; [ImportingConstructor] public Log4NetFramework(ILoggingMediator loggingService) { _loggingService = loggingService; _loggingService.Listen(this); XmlConfigurator.Configure(); } /// <summary> /// The log4net logger which this class wraps. /// </summary> private ILog _logger; /// <summary> /// Logs the given message. Output depends on the associated /// log4net configuration. /// </summary> /// <param name="item">A <see cref="LogItem"/> which encapsulates /// information to be logged.</param> /// <exception cref="ArgumentNullException">If <paramref name="item"/> /// is a null reference.</exception> public override void Log(LogItem item) { if (item == null) throw new ArgumentNullException("item"); string message = FormatItem(item); _logger = LogManager.GetLogger(item.LoggerName); switch (item.LogLevel) { case LogLevel.Fatal: _logger.Fatal(message, item.Exception); break; case LogLevel.Error: _logger.Error(message, item.Exception); break; case LogLevel.Warn: _logger.Warn(message, item.Exception); break; case LogLevel.Info: _logger.Info(message, item.Exception); break; case LogLevel.Debug: _logger.Debug(message, item.Exception); break; default: _logger.Info(message, item.Exception); break; } } /// <summary> /// Overriden to delegate to the log4net IsXxxEnabled /// properties. /// </summary> protected override bool IsLogLevelEnabled(LogLevel level) { switch (level) { case LogLevel.Debug: return _logger.IsDebugEnabled; case LogLevel.Error: return _logger.IsErrorEnabled; case LogLevel.Fatal: return _logger.IsFatalEnabled; case LogLevel.Info: return _logger.IsInfoEnabled; case LogLevel.Warn: return _logger.IsWarnEnabled; default: return true; } } [LoggingMessageSink] public void OnLoggingMessageArrive(LogItem item) { this.Log(item); } } ************************************************************************************* And in the class that you want logging you just do like this: [ImportingConstructor] public SelectedUserViewModel(ILoggingMediator loggingService) { _loggingService = loggingService; _loggingService.InitLogger(this, "LoggerName"); } public void TestMethodThatLogsException() { var exception = new System.Exception("This is an error", new Exception("This is an inner exception")); _loggerService.Error(exception, "An error has occurred in method [{0}]. [Parameters:obj = " + obj + "]. Please contact your Administrator", new object[]{"OnExecuteGetWatchers"}); } ************************************************************************************ This way i'm free to catch this logmessage whereever I want, and even have more then one "listners" that do different things. Mayby you should be able to send to a specific or a collection of "Log Framworks". Right now it's sent to all that listen to the message "LogEntry". I'm not sure about the performane doing it this way. Do you think it can be a problem?
Jul 6, 2010 at 9:30 AM
That message didn't look good. I will try fix it.
Jul 6, 2010 at 9:34 AM
Don't know how to have it look better.
Coordinator
Jul 8, 2010 at 9:10 PM
I do not think that it should be using the mediator... I think it should be just a normal service.... Can you send me the code and I'll have a look please
Jul 9, 2010 at 2:15 PM
Yes I can. Give me some time and I will send you my code.