aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Logging/LogWindow.xaml.cs
blob: 3dc11db5b24fc20c385ddac0eb7600a9303943e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using MediaBrowser.Common.Kernel;
using NLog;
using NLog.Config;
using NLog.Targets;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace MediaBrowser.Common.Logging
{
    /// <summary>
    /// Interaction logic for LogWindow.xaml
    /// </summary>
    public partial class LogWindow : Window
    {
        /// <summary>
        /// The _ui thread
        /// </summary>
        private readonly TaskScheduler _uiThread;
        /// <summary>
        /// The _kernel
        /// </summary>
        private readonly IKernel _kernel;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogWindow" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public LogWindow(IKernel kernel)
        {
            InitializeComponent();
            _uiThread = TaskScheduler.FromCurrentSynchronizationContext();
            _kernel = kernel;

            Loaded += LogWindow_Loaded;
        }

        /// <summary>
        /// Handles the Loaded event of the LogWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        void LogWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var target = new TraceTarget
            {
                Layout = "${longdate}, ${level}, ${logger}, ${message}"
            };

            AddLogTarget(target, "LogWindowTraceTarget");
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Window.Closing" /> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            RemoveLogTarget("LogWindowTraceTarget");
        }

        /// <summary>
        /// Logs the message.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        public async void LogMessage(string msg)
        {
            await Task.Factory.StartNew(() => lbxLogData.Items.Insert(0, msg.TrimEnd('\n')), CancellationToken.None, TaskCreationOptions.None, _uiThread);
        }

        /// <summary>
        /// The log layout
        /// </summary>
        /// <value>The log layout.</value>
        public string LogLayout
        {
            get { return "${longdate}, ${level}, ${logger}, ${message}"; }
        }

        /// <summary>
        /// Adds the log target.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="name">The name.</param>
        private void AddLogTarget(Target target, string name)
        {
            var config = NLog.LogManager.Configuration;

            config.RemoveTarget(name);

            target.Name = name;
            config.AddTarget(name, target);

            var level = _kernel.Configuration.EnableDebugLevelLogging ? LogLevel.Debug : LogLevel.Info;

            var rule = new LoggingRule("*", level, target);
            config.LoggingRules.Add(rule);

            NLog.LogManager.Configuration = config;
        }

        /// <summary>
        /// Removes the log target.
        /// </summary>
        /// <param name="name">The name.</param>
        private void RemoveLogTarget(string name)
        {
            var config = NLog.LogManager.Configuration;

            config.RemoveTarget(name);

            NLog.LogManager.Configuration = config;
        }
        
        /// <summary>
        /// Shuts down.
        /// </summary>
        public async void ShutDown()
        {
            await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread);
        }

    }

}