Skip to content

Commit

Permalink
Merge pull request #12 from dojo90/11-implement-custom-message-resolver
Browse files Browse the repository at this point in the history
11 implement custom message resolver
  • Loading branch information
djonasdev committed Feb 10, 2020
2 parents 7b8bcad + f0af91a commit 101374f
Show file tree
Hide file tree
Showing 24 changed files with 1,501 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[3]: https://github.com/yarseyah/sentinel#nlogs-nlogviewer-target-configuration

[p1]: doc/images/control.png "NLogViewer"
[p2]: doc/images/live.gif "NLogViewer"
[p2]: doc/images/preview.gif "NLogViewer"
[p3]: doc/images/control2.png "NLogViewer"

[nuget]: https://nuget.org/packages/Sentinel.NlogViewer/
Expand Down
Binary file removed doc/images/live.gif
Binary file not shown.
Binary file added doc/images/preview.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion src/NLogViewer.TestApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
xmlns:dj="clr-namespace:DJ;assembly=NLogViewer"
mc:Ignorable="d" Height="450" Width="800">
<Grid>
<dj:NLogViewer MaxCount="1000"/>
<Grid.RowDefinitions>
<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"/>
</Grid>
</Window>
41 changes: 37 additions & 4 deletions src/NLogViewer.TestApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Windows;
using DJ.Resolver;
using NLog;

namespace TestApplication
Expand All @@ -10,21 +12,35 @@ namespace TestApplication
/// </summary>
public partial class MainWindow : Window
{
public const string LOREM_IPSUM = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

private readonly Logger _Logger = LogManager.GetCurrentClassLogger();
private readonly Logger _Logger2 = LogManager.GetLogger("Lorem.Ipsum.Foo.Hello.World.Lorem.Ipsum");
public MainWindow()
{
Title = $"Testing v{AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}";
InitializeComponent();
DataContext = this;

NLogViewer1.TimeStampResolver = new FooTimeStampResolver();
Stopwatch stopwatch = Stopwatch.StartNew();
Random random = new Random();
Observable.Interval(TimeSpan.FromMilliseconds(250)).ObserveOnDispatcher().Subscribe(l =>
Observable.Interval(TimeSpan.FromMilliseconds(200)).ObserveOnDispatcher().Subscribe(l =>
{
switch (random.Next(1,6))
{
case 1:
_Logger.Trace("Hello everyone");
break;
case 2:
_Logger.Debug("Hello everyone");
if (stopwatch.Elapsed.Seconds > 5)
{
_Logger2.Debug("Hello everyone");
}
else
{
_Logger.Debug("Hello everyone");
}
break;
case 3:
_Logger.Info("Hello everyone");
Expand All @@ -33,13 +49,30 @@ public MainWindow()
_Logger.Warn("Hello everyone");
break;
case 5:
_Logger.Error("Hello everyone");
try
{
int a = 0;
int b = 1;
var c = b / a;
}
catch (Exception ex)
{
_Logger.Error(ex, "There was an error on divison :/");
}
break;
case 6:
_Logger.Fatal("Hello everyone");
_Logger.Fatal(LOREM_IPSUM);
break;
}
});
}
}

public class FooTimeStampResolver : ILogEventInfoResolver
{
public string Resolve(LogEventInfo logEventInfo)
{
return logEventInfo.TimeStamp.ToUniversalTime().ToString();
}
}
}
8 changes: 4 additions & 4 deletions src/NLogViewer.TestApp/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
</extensions>

<targets async="true">
<target
xsi:type="CacheTarget"
name="cache"/>
<target xsi:type="CacheTarget" name="target1"/>
<target xsi:type="CacheTarget" name="target2"/>
</targets>

<rules>
<logger name="*" writeTo="cache" minlevel="Debug"/>
<logger name="*" writeTo="target1" maxlevel="Info"/>
<logger name="*" writeTo="target2" minlevel="Warn"/>
</rules>
</nlog>
42 changes: 42 additions & 0 deletions src/NLogViewer/Helper/AutoSizedGridView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Reactive.Linq;
using System.Windows.Controls;
using NLog;

namespace DJ.Helper
{
/// <summary>
/// Represents a view mode that displays data items in columns for a System.Windows.Controls.ListView control with auto sized columns based on the column content
/// Used to fix the column width: https://stackoverflow.com/questions/60147905/column-width-adjustment-is-broken-if-using-multibinding-on-displaymemberbinding
/// </summary>
public class AutoSizedGridView : GridView
{
private int _MaxLoggerNameLength;

protected override void PrepareItem(ListViewItem item)
{
if (item.DataContext is LogEventInfo info)
{
if (info.LoggerName.Length > _MaxLoggerNameLength)
{
_MaxLoggerNameLength = info.LoggerName.Length;
Observable.Timer(TimeSpan.FromMilliseconds(1)).ObserveOnDispatcher().Subscribe(l =>
{
foreach (GridViewColumn column in Columns)
{
//setting NaN for the column width automatically determines the required width enough to hold the content completely.
//if column width was set to NaN already, set it ActualWidth temporarily and set to NaN. This raises the property change event and re computes the width.
if (double.IsNaN(column.Width))
{
column.Width = column.ActualWidth;
column.Width = double.NaN;
}
}
});
}
}

base.PrepareItem(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;

namespace DJ.Helper.ListViewLayoutManager
{
public abstract class ConverterGridViewColumn : GridViewColumn, IValueConverter
{
public Type BindingType => _BindingType;
private readonly Type _BindingType;

// ##############################################################################################################################
// Constructor
// ##############################################################################################################################

#region Constructor

protected ConverterGridViewColumn(Type bindingType)
{
if (bindingType == null)
{
throw new ArgumentNullException(nameof(bindingType));
}

this._BindingType = bindingType;

Binding binding = new Binding {Mode = BindingMode.OneWay, Converter = this};
DisplayMemberBinding = binding;
}

#endregion

// ##############################################################################################################################
// IValueConverter
// ##############################################################################################################################

#region IValueConverter

object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!_BindingType.IsInstanceOfType(value))
{
throw new InvalidOperationException();
}

return ConvertValue(value);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

protected abstract object ConvertValue(object value);

#endregion
}
}
61 changes: 61 additions & 0 deletions src/NLogViewer/Helper/ListViewLayoutManager/FixedColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Windows;
using System.Windows.Controls;

namespace DJ.Helper.ListViewLayoutManager
{
public sealed class FixedColumn : LayoutColumn
{
public static bool IsFixedColumn(GridViewColumn column)
{
if (column == null)
{
return false;
}

return HasPropertyValue(column, WidthProperty);
}

public static double? GetFixedWidth(GridViewColumn column)
{
return GetColumnWidth(column, WidthProperty);
}

public static GridViewColumn ApplyWidth(GridViewColumn gridViewColumn, double width)
{
SetWidth(gridViewColumn, width);
return gridViewColumn;
}

// ##############################################################################################################################
// AttachedProperties
// ##############################################################################################################################

#region AttachedProperties

public static double GetWidth(DependencyObject obj)
{
return (double) obj.GetValue(WidthProperty);
}

public static void SetWidth(DependencyObject obj, double width)
{
obj.SetValue(WidthProperty, width);
}

public static readonly DependencyProperty WidthProperty = DependencyProperty.RegisterAttached("Width", typeof(double), typeof(FixedColumn));

#endregion

// ##############################################################################################################################
// Constructor
// ##############################################################################################################################

#region Constructor

private FixedColumn()
{
}

#endregion
}
}
58 changes: 58 additions & 0 deletions src/NLogViewer/Helper/ListViewLayoutManager/ImageGridViewColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace DJ.Helper.ListViewLayoutManager
{
public abstract class ImageGridViewColumn : GridViewColumn, IValueConverter
{
// ##############################################################################################################################
// Constructor
// ##############################################################################################################################

#region Constructor

protected ImageGridViewColumn(Stretch imageStretch)
{
FrameworkElementFactory imageElement = new FrameworkElementFactory(typeof(Image));

Binding imageSourceBinding = new Binding {Converter = this, Mode = BindingMode.OneWay};
imageElement.SetBinding(Image.SourceProperty, imageSourceBinding);

Binding imageStretchBinding = new Binding {Source = imageStretch};
imageElement.SetBinding(Image.StretchProperty, imageStretchBinding);

DataTemplate template = new DataTemplate {VisualTree = imageElement};
CellTemplate = template;
}

protected ImageGridViewColumn() : this(Stretch.None)
{
}

#endregion

// ##############################################################################################################################
// IValueConverter
// ##############################################################################################################################

#region IValueConverter

object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return GetImageSource(value);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

protected abstract ImageSource GetImageSource(object value);

#endregion
}
}
41 changes: 41 additions & 0 deletions src/NLogViewer/Helper/ListViewLayoutManager/LayoutColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Windows;
using System.Windows.Controls;

namespace DJ.Helper.ListViewLayoutManager
{
public abstract class LayoutColumn
{
protected static bool HasPropertyValue(GridViewColumn column, DependencyProperty dp)
{
if (column == null)
{
throw new ArgumentNullException(nameof(column));
}

object value = column.ReadLocalValue(dp);
if (value?.GetType() == dp.PropertyType)
{
return true;
}

return false;
}

protected static double? GetColumnWidth(GridViewColumn column, DependencyProperty dp)
{
if (column == null)
{
throw new ArgumentNullException(nameof(column));
}

object value = column.ReadLocalValue(dp);
if (value?.GetType() == dp.PropertyType)
{
return (double) value;
}

return null;
}
}
}
Loading

0 comments on commit 101374f

Please sign in to comment.