Skip to content

Commit

Permalink
add nlog with basic config
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzzerd committed Sep 29, 2023
1 parent 22990ad commit 8a97f9d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
6 changes: 5 additions & 1 deletion SharpFM.App/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using SharpFM.App.ViewModels;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

namespace SharpFM.App;

public partial class App : Application
{
ILogger logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<MainWindow>();

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Expand All @@ -18,7 +22,7 @@ public override void OnFrameworkInitializationCompleted()
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
DataContext = new MainWindowViewModel(logger)
};
}

Expand Down
5 changes: 5 additions & 0 deletions SharpFM.App/SharpFM.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

<PackageReference Include="NLog" Version="5.2.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.4" />

<None Update="nlog.config" CopyToOutputDirectory="Always" />

<ProjectReference Include="..\SharpFM.Core\SharpFM.Core.csproj" />
</ItemGroup>
Expand Down
15 changes: 12 additions & 3 deletions SharpFM.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using FluentAvalonia.UI.Data;
using Microsoft.Extensions.Logging;
using SharpFM.App.Models;
using SharpFM.Core;

namespace SharpFM.App.ViewModels;

public partial class MainWindowViewModel : INotifyPropertyChanged
{
public ClipDbContext _context;
private readonly ILogger _logger;

public readonly ClipDbContext _context;

public event PropertyChangedEventHandler? PropertyChanged;

Expand All @@ -23,12 +26,14 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public MainWindowViewModel()
public MainWindowViewModel(ILogger logger)
{
_logger = logger;

_context = new ClipDbContext();
_context.Database.EnsureCreated();

Console.WriteLine($"Database path: {_context.DbPath}.");
_logger.LogInformation($"Database path: {_context.DbPath}.");

FileMakerClips = new ObservableCollection<ClipViewModel>();

Expand Down Expand Up @@ -104,6 +109,7 @@ public void NewEmptyItem()
}
catch (Exception e)
{
_logger.LogCritical("Error creating new Clip.", e);
}
}

Expand All @@ -129,6 +135,7 @@ public void CopyAsClass()
}
catch (Exception e)
{
_logger.LogCritical("Error Copying as Class.", e);
}
}

Expand Down Expand Up @@ -171,6 +178,7 @@ public async Task PasteFileMakerClipData()
}
catch (Exception e)
{
_logger.LogCritical("Error translating FileMaker blob to Xml.", e);
}
}

Expand All @@ -196,6 +204,7 @@ public async Task CopySelectedToClip()
}
catch (Exception e)
{
_logger.LogCritical("Error returning the selected Clip FileMaker blob format.", e);
}
}

Expand Down
35 changes: 35 additions & 0 deletions SharpFM.App/nlog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info">

<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<!-- write logs to the visual studio code "debug console" -->
<target name="logconsole" xsi:type="ColoredConsole" enableAnsiOutput="true"
layout="${time}|${uppercase:${level:padding=5}}|${mdlc:item=ScopeName}|${ndlctiming:currentScope=true}|${logger}|${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=2}"
useDefaultRowHighlightingRules="true">
<!-- explicitly use nlog color coding with a few adjustments for consistent display -->
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" />
<highlight-row condition="level == LogLevel.Error" foregroundColor="Yellow" />
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Magenta" />
<highlight-row condition="level == LogLevel.Info" foregroundColor="Cyan" />
<highlight-row condition="level == LogLevel.Debug" foregroundColor="Green" />
<highlight-row condition="level == LogLevel.Trace" foregroundColor="DarkGray" />
</target>

<target xsi:type="File" name="logfile"
fileName="${specialfolder:folder=ApplicationData}\SharpFM.log"
layout="${longdate}|${uppercase:${level:padding=5}}|${mdlc:item=ScopeName}|${ndlctiming:currentScope=true}|${logger}|${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=2}" />
</targets>

<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile,logconsole" />
</rules>
</nlog>

0 comments on commit 8a97f9d

Please sign in to comment.