aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/Logging/LogForm.cs
blob: a8813406a009117fb48729e997be0efe42ed1bce (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
using MediaBrowser.Common.Implementations.Logging;
using MediaBrowser.Model.Logging;
using NLog.Targets;
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaBrowser.ServerApplication.Logging
{
    public partial class LogForm : Form
    {
        private readonly TaskScheduler _uiThread;
        private readonly ILogManager _logManager;
        
        public LogForm(ILogManager logManager)
        {
            InitializeComponent();

            _logManager = logManager;
            _uiThread = TaskScheduler.FromCurrentSynchronizationContext();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ((NlogManager)_logManager).RemoveTarget("LogWindowTraceTarget");

            ((NlogManager)_logManager).AddLogTarget(new TraceTarget
            {
                Layout = "${longdate}, ${level}, ${logger}, ${message}",
                Name = "LogWindowTraceTarget"

            }, LogSeverity.Debug);
        }

        /// <summary>
        /// Logs the message.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        public async void LogMessage(string msg)
        {
            await Task.Factory.StartNew(() =>
            {
                if (listBox1.Items.Count > 10000)
                {
                    //I think the quickest and safest thing to do here is just clear it out
                    listBox1.Items.Clear();
                }

                foreach (var line in msg.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        listBox1.Items.Insert(0, line);
                    }
                }

            }, 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>
        /// Shuts down.
        /// </summary>
        public async void ShutDown()
        {
            await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            ((NlogManager)_logManager).RemoveTarget("LogWindowTraceTarget");
        }
    }
}