//
// Copyright 2014 Perforce Software Inc.
//
using log4net.Appender;
using log4net.Core;
using log4net.Repository.Hierarchy;
using System.ComponentModel;
using System.Globalization;
using System.IO;
namespace Perforce {
public class NotifyAppender : AppenderSkeleton, INotifyPropertyChanged {
#region Members and events
private static string _notification;
private event PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged {
add { _propertyChanged += value; }
remove { _propertyChanged -= value; }
}
#endregion
///
/// Get or set the notification message.
///
public string Notification {
get {
return _notification; ;
}
set {
if (_notification != value) {
_notification = value;
OnChange();
}
}
}
public LogLevel CurrentLogLevel {
get {
var h = (Hierarchy)log4net.LogManager.GetRepository();
var level = h.Root.Level;
var logLevel = LogLevel.Fatal;
if (level == Level.All || level == Level.Debug) {
logLevel = LogLevel.Debug;
} else if (level == Level.Error) {
logLevel = LogLevel.Error;
} else if (level == Level.Warn) {
logLevel = LogLevel.Warning;
} else if (level == Level.Info) {
logLevel = LogLevel.Info;
}
return logLevel;
}
set {
var h = (Hierarchy)log4net.LogManager.GetRepository();
switch (value) {
case LogLevel.Debug:
h.Root.Level = Level.Debug;
break;
case LogLevel.Error:
h.Root.Level = Level.Error;
break;
case LogLevel.Fatal:
h.Root.Level = Level.Fatal;
break;
case LogLevel.Info:
h.Root.Level = Level.Info;
break;
case LogLevel.Warning:
h.Root.Level = Level.Warn;
break;
}
OnChange();
}
}
///
/// Raise the change notification.
///
private void OnChange() {
PropertyChangedEventHandler handler = _propertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(string.Empty));
}
}
///
/// Get a reference to the log instance.
///
public NotifyAppender Appender {
get {
return Log.Appender;
}
}
///
/// Append the log information to the notification.
///
/// The log event.
protected override void Append(LoggingEvent loggingEvent) {
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
Layout.Format(writer, loggingEvent);
Notification += writer.ToString();
}
}
}