LogWindow.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using MediaBrowser.Common.Implementations.Logging;
  2. using MediaBrowser.Model.Logging;
  3. using NLog;
  4. using NLog.Config;
  5. using NLog.Targets;
  6. using System.ComponentModel;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. namespace MediaBrowser.ServerApplication.Logging
  11. {
  12. /// <summary>
  13. /// Interaction logic for LogWindow.xaml
  14. /// </summary>
  15. public partial class LogWindow : Window
  16. {
  17. /// <summary>
  18. /// The _ui thread
  19. /// </summary>
  20. private readonly TaskScheduler _uiThread;
  21. private readonly ILogManager _logManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="LogWindow" /> class.
  24. /// </summary>
  25. /// <param name="kernel">The kernel.</param>
  26. public LogWindow(ILogManager logManager)
  27. {
  28. InitializeComponent();
  29. _uiThread = TaskScheduler.FromCurrentSynchronizationContext();
  30. _logManager = logManager;
  31. Loaded += LogWindow_Loaded;
  32. }
  33. /// <summary>
  34. /// Handles the Loaded event of the LogWindow control.
  35. /// </summary>
  36. /// <param name="sender">The source of the event.</param>
  37. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  38. void LogWindow_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. ((NlogManager)_logManager).RemoveTarget("LogWindowTraceTarget");
  41. ((NlogManager)_logManager).AddLogTarget(new TraceTarget
  42. {
  43. Layout = "${longdate}, ${level}, ${logger}, ${message}",
  44. Name = "LogWindowTraceTarget"
  45. }, LogSeverity.Debug);
  46. }
  47. /// <summary>
  48. /// Raises the <see cref="E:System.Windows.Window.Closing" /> event.
  49. /// </summary>
  50. /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
  51. protected override void OnClosing(CancelEventArgs e)
  52. {
  53. base.OnClosing(e);
  54. ((NlogManager) _logManager).RemoveTarget("LogWindowTraceTarget");
  55. }
  56. /// <summary>
  57. /// Logs the message.
  58. /// </summary>
  59. /// <param name="msg">The MSG.</param>
  60. public async void LogMessage(string msg)
  61. {
  62. await Task.Factory.StartNew(() =>
  63. {
  64. if (lbxLogData.Items.Count > 10000)
  65. {
  66. //I think the quickest and safest thing to do here is just clear it out
  67. lbxLogData.Items.Clear();
  68. }
  69. lbxLogData.Items.Insert(0, msg.TrimEnd('\n'));
  70. }, CancellationToken.None, TaskCreationOptions.None, _uiThread);
  71. }
  72. /// <summary>
  73. /// The log layout
  74. /// </summary>
  75. /// <value>The log layout.</value>
  76. public string LogLayout
  77. {
  78. get { return "${longdate}, ${level}, ${logger}, ${message}"; }
  79. }
  80. /// <summary>
  81. /// Adds the log target.
  82. /// </summary>
  83. /// <param name="target">The target.</param>
  84. /// <param name="name">The name.</param>
  85. private void AddLogTarget(Target target, string name)
  86. {
  87. var config = NLog.LogManager.Configuration;
  88. target.Name = name;
  89. config.AddTarget(name, target);
  90. var level = LogLevel.Debug;
  91. var rule = new LoggingRule("*", level, target);
  92. config.LoggingRules.Add(rule);
  93. NLog.LogManager.Configuration = config;
  94. }
  95. /// <summary>
  96. /// Shuts down.
  97. /// </summary>
  98. public async void ShutDown()
  99. {
  100. await Task.Factory.StartNew(Close, CancellationToken.None, TaskCreationOptions.None, _uiThread);
  101. }
  102. }
  103. }