2
0

LogWindow.xaml.cs 3.6 KB

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