Skip to content

Commit

Permalink
Merge pull request #20 from dojo90/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
djonasdev committed Feb 19, 2020
2 parents 5bf556d + 52fe2ed commit b4b2b0a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 37 deletions.
33 changes: 20 additions & 13 deletions src/NLogViewer.TestApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@
mc:Ignorable="d" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="5" />
<RowDefinition Height="*" />
<RowDefinition Height="5" />
<RowDefinition Height="Auto" />
<RowDefinition Height="5" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Target 1" FontSize="18"></TextBlock>
<dj:NLogViewer x:Name="NLogViewer1" Grid.Row="2" MaxCount="1000" TargetName="target1"/>
<TextBlock Grid.Row="4" Text="Target 2 (Warn and Error)" FontSize="18"></TextBlock>
<dj:NLogViewer Grid.Row="6" MaxCount="1000" TargetName="target2"/>
<Button Grid.Row="0" Grid.RowSpan="2" Margin="5" Content="Open Popup" HorizontalAlignment="Right" VerticalAlignment="Top" Click="ButtonBase_OnClick"/>
<TabControl Grid.RowSpan="10" x:Name="TabControl1">
<TabItem Header="Messages">
<dj:NLogViewer x:Name="NLogViewer1" Grid.Row="2" MaxCount="1000" TargetName="target1" />
</TabItem>
<TabItem Header="Warnings/Errors">
<dj:NLogViewer Grid.Row="6" MaxCount="1000" TargetName="target2" />
</TabItem>
</TabControl>
<!--<TextBlock Grid.Row="0" Text="Target 1" FontSize="18" />
<TextBlock Grid.Row="4" Text="Target 2 (Warn and Error)" FontSize="18" />
<Button Grid.Row="0" Grid.RowSpan="2" Margin="5" Content="Open Popup" HorizontalAlignment="Right"
VerticalAlignment="Top" Click="ButtonBase_OnClick" />-->
</Grid>
</Window>
</Window>
28 changes: 21 additions & 7 deletions src/NLogViewer.TestApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public partial class MainWindow : Window

private readonly Logger _Logger = LogManager.GetCurrentClassLogger();
private readonly Logger _Logger2 = LogManager.GetLogger("Lorem.Ipsum.Foo.Hello.World.Lorem.Ipsum");

private int _CntMessage;
private int _CntError;


public MainWindow()
{
Title = $"Testing v{AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}";
Expand All @@ -27,28 +32,36 @@ public MainWindow()
Random random = new Random();
Observable.Interval(TimeSpan.FromMilliseconds(200)).ObserveOnDispatcher().Subscribe(l =>
{
if((_CntMessage == 10 || _CntError == 20) && TabControl1.Items.Count > 0)
TabControl1.Items.RemoveAt(0);
switch (random.Next(1,6))
{
case 1:
_Logger.Trace("Hello everyone");
_CntMessage++;
_Logger.Trace($"Hello everyone: {_CntMessage}");
break;
case 2:
_CntMessage++;
if (stopwatch.Elapsed.Seconds > 5)
{
_Logger2.Debug("Hello everyone");
_Logger2.Debug($"Hello everyone: {_CntMessage}");
}
else
{
_Logger.Debug("Hello everyone");
_Logger.Debug($"Hello everyone: {_CntMessage}");
}
break;
case 3:
_Logger.Info("Hello everyone");
_CntMessage++;
_Logger.Info($"Hello everyone: {_CntMessage}");
break;
case 4:
_Logger.Warn("Hello everyone");
_CntError++;
_Logger.Warn($"Hello everyone: {_CntError}");
break;
case 5:
_CntError++;
try
{
int a = 0;
Expand All @@ -57,11 +70,12 @@ public MainWindow()
}
catch (Exception ex)
{
_Logger.Error(ex, "There was an error on divison :/");
_Logger.Error(ex, $"There was an error on divison :/ {_CntError}");
}
break;
case 6:
_Logger.Fatal(LOREM_IPSUM);
_CntError++;
_Logger.Fatal($"{_CntError}\n{LOREM_IPSUM}");
break;
}
});
Expand Down
72 changes: 72 additions & 0 deletions src/NLogViewer/Extensions/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace DJ.Extensions
{
public static class DependencyObjectExtensions
{
/// <summary>
/// Analyzes both visual and logical tree in order to find all elements of a given
/// type that are descendants of the <paramref name="source"/> item.
/// </summary>
/// <typeparam name="T">The type of the queried items.</typeparam>
/// <param name="source">The root element that marks the source of the search. If the
/// source is already of the requested type, it will not be included in the result.</param>
/// <param name="uid">The UID of the <see cref="UIElement"/></param>
/// <returns>All descendants of <paramref name="source"/> that match the requested type.</returns>
public static T FindChildByUid<T>(this DependencyObject source, string uid) where T : UIElement
{
if (source != null)
{
var childs = GetChildObjects(source);
foreach (DependencyObject child in childs)
{
//analyze if children match the requested type
if (child != null && child is T dependencyObject && dependencyObject.Uid.Equals(uid))
{
return dependencyObject;
}

var descendant = FindChildByUid<T>(child, uid);
if (descendant != null)
return descendant;
}
}

return null;
}

/// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetChild"/> method, which also
/// supports content elements. Keep in mind that for content elements,
/// this method falls back to the logical tree of the element.
/// </summary>
/// <param name="parent">The item to be processed.</param>
/// <returns>The submitted item's child elements, if available.</returns>
public static IEnumerable<DependencyObject> GetChildObjects(this DependencyObject parent)
{
if (parent == null) yield break;

if (parent is ContentElement || parent is FrameworkElement)
{
//use the logical tree for content / framework elements
foreach (object obj in LogicalTreeHelper.GetChildren(parent))
{
var depObj = obj as DependencyObject;
if (depObj != null) yield return (DependencyObject) obj;
}
}
else
{
//use the visual tree per default
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
yield return VisualTreeHelper.GetChild(parent, i);
}
}
}
}
}
34 changes: 17 additions & 17 deletions src/NLogViewer/NLogViewer.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
Expand All @@ -12,6 +10,7 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using DJ.Extensions;
using DJ.Resolver;
using DJ.Targets;
using NLog;
Expand Down Expand Up @@ -445,7 +444,8 @@ public ILogEventInfoResolver MessageResolver
// ##########################################################################################

private ObservableCollection<LogEventInfo> _LogEventInfos { get; } = new ObservableCollection<LogEventInfo>();
private IDisposable _Subscription;
private IDisposable _Subscription;
private Window _ParentWindow;

#endregion

Expand All @@ -459,30 +459,32 @@ public NLogViewer()
{
InitializeComponent();
DataContext = this;

// save instance UID
Uid = GetHashCode().ToString();

if (DesignerProperties.GetIsInDesignMode(this))
return;

LogEvents = new CollectionViewSource {Source = _LogEventInfos};

Loaded += _OnLoaded;
Unloaded += _OnUnloaded;
ClearCommand = new ActionCommand(_LogEventInfos.Clear);
}

private void _ParentWindowOnClosed(object? sender, EventArgs e)
{
_Dispose();
}

// check if the parent visual has been changed
// can happen if you use the control on a page
protected override void OnVisualParentChanged(DependencyObject oldParent)
private void _OnUnloaded(object sender, RoutedEventArgs e)
{
if (oldParent != null)
// look in logical and visual tree if the control has been removed
if (_ParentWindow.FindChildByUid<NLogViewer>(Uid) == null)
{
_Dispose();
}
base.OnVisualParentChanged(oldParent);
}

private void _ParentWindowOnClosed(object sender, EventArgs e)
{
_Dispose();
}

private void _Dispose()
Expand All @@ -496,12 +498,10 @@ private void _OnLoaded(object sender, RoutedEventArgs e)
Loaded -= _OnLoaded;

// add hook to parent window to dispose subscription
var parentWindow = Window.GetWindow(this);
if(parentWindow != null)
parentWindow.Closed += _ParentWindowOnClosed;
_ParentWindow = Window.GetWindow(this);
_ParentWindow.Closed += _ParentWindowOnClosed;

ListView.ScrollToEnd();

var target = CacheTarget.GetInstance(targetName: TargetName);

_Subscription = target.Cache.SubscribeOn(Scheduler.Default).Buffer(TimeSpan.FromMilliseconds(100)).Where (x => x.Any()).ObserveOnDispatcher(DispatcherPriority.Background).Subscribe(infos =>
Expand Down

0 comments on commit b4b2b0a

Please sign in to comment.