Skip to content

Commit

Permalink
Improved ZeroMQSubscriber logging capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
qjingjie committed Feb 3, 2025
1 parent 0f1d049 commit 90a66d9
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ZeroMQSubscriber/App.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Application x:Class="ZeroMQSubscriber.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ZeroMQSubscriber">
<Application x:Class="ZeroMQSubscriber.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
3 changes: 0 additions & 3 deletions ZeroMQSubscriber/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;
using Utils.Interfaces;
using ZeroMQSubscriber.Models;
using ZeroMQSubscriber.ViewModels;

Expand Down
2 changes: 1 addition & 1 deletion ZeroMQSubscriber/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
)]
8 changes: 1 addition & 7 deletions ZeroMQSubscriber/Enums/ConnectionState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZeroMQSubscriber.Enums
namespace ZeroMQSubscriber.Enums
{
public enum ConnectionState
{
Expand Down
8 changes: 1 addition & 7 deletions ZeroMQSubscriber/Enums/ConnectionType.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZeroMQSubscriber.Enums
namespace ZeroMQSubscriber.Enums
{
public enum ConnectionType
{
Expand Down
6 changes: 4 additions & 2 deletions ZeroMQSubscriber/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="ZeroMQSubscriber.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:svgc="http://sharpvectors.codeplex.com/svgc/" xmlns:validators="clr-namespace:Utils.ValidationRules;assembly=Utils" xmlns:enums="clr-namespace:ZeroMQSubscriber.Enums" mc:Ignorable="d" Title="ZeroMQ Subscriber" FontFamily="{StaticResource Ubuntu}" ResizeMode="CanResize" SizeToContent="WidthAndHeight" MinWidth="600" MinHeight="250" Style="{StaticResource CustomWindow}">
<Window x:Class="ZeroMQSubscriber.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:svgc="http://sharpvectors.codeplex.com/svgc/" xmlns:validators="clr-namespace:Utils.ValidationRules;assembly=Utils" xmlns:enums="clr-namespace:ZeroMQSubscriber.Enums" mc:Ignorable="d" Title="ZeroMQ Subscriber" FontFamily="{StaticResource Ubuntu}" ResizeMode="CanResize" SizeToContent="WidthAndHeight" MinWidth="600" MinHeight="250" Style="{StaticResource CustomWindow}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
Expand Down Expand Up @@ -450,7 +450,9 @@

<TextBlock Foreground="{StaticResource Neutral-200}" FontSize="{StaticResource Body2}" TextWrapping="WrapWithOverflow" Margin="0 10 0 0">
<Run Text="File saved to: " />
<Run Text="{Binding LogFilePath}" />
<Hyperlink Command="{Binding OpenLogFileCommand}">
<Run Text="{Binding LogFilePath}" />
</Hyperlink>

<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
Expand Down
10 changes: 1 addition & 9 deletions ZeroMQSubscriber/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZeroMQSubscriber
{
Expand Down
7 changes: 0 additions & 7 deletions ZeroMQSubscriber/Models/Subscriber.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using ZeroMQSubscriber.Enums;

namespace ZeroMQSubscriber.Models
Expand Down
72 changes: 59 additions & 13 deletions ZeroMQSubscriber/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using Serilog;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Printing.IndexedProperties;
using System.Text;
using System.Threading.Tasks;
using Utils;
using ZeroMQSubscriber.Enums;
using ZeroMQSubscriber.Models;

Expand All @@ -20,6 +16,7 @@ public partial class MainViewModel : ObservableObject
private readonly Subscriber _subscriber;

private string _folderPath;
private string _uriPath;

#endregion Fields

Expand All @@ -29,13 +26,15 @@ public MainViewModel(Subscriber subscriber)
{
_subscriber = subscriber;

_folderPath = CreateDefaultDirectory();
_uriPath = string.Empty;

Ipv4 = string.Empty;
Port = string.Empty;
Topic = string.Empty;
ReceivedMessage = string.Empty;
LogFileName = string.Empty;

CreateDefaultDirectory();
LogFilePath = string.Empty;
}

#endregion Constructor
Expand Down Expand Up @@ -106,15 +105,30 @@ private void Unsubscribe()
private void StartLogging()
{
IsLogging = true;
LogFilePath = Path.Combine(_folderPath, $"{LogFileName}.txt");
ConfigureLogger();
ShowLogFilePath = false;
}

[RelayCommand]
private void StopLogging()
{
IsLogging = false;
ShowLogFilePath = true;
Log.CloseAndFlush();

string latestLogFileName = GetLatestLogFileName();

if (latestLogFileName != null)
{
LogFilePath = Path.Combine(_folderPath, latestLogFileName);
_uriPath = new Uri(LogFilePath).AbsoluteUri;
ShowLogFilePath = true;
}
}

[RelayCommand]
private void OpenLogFile()
{
Process.Start("explorer.exe", _uriPath);
}

[RelayCommand]
Expand All @@ -129,6 +143,11 @@ private async Task WindowClosingAsync()
{
await _subscriber.Configure(ConnectionType.Unbind, Ipv4, int.Parse(Port));
}

if (IsLogging)
{
Log.CloseAndFlush();
}
}

#endregion Commands
Expand All @@ -138,7 +157,8 @@ private async Task WindowClosingAsync()
/// <summary>
/// Create a directory for storing log files in appdata.
/// </summary>
private void CreateDefaultDirectory()
/// <returns>Created folder path.</returns>
private static string CreateDefaultDirectory()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string folderPath = Path.Combine(appDataPath, "SimpleTools", "ZeroMQ", "Logs");
Expand All @@ -148,7 +168,33 @@ private void CreateDefaultDirectory()
Directory.CreateDirectory(folderPath);
}

_folderPath = folderPath;
return folderPath;
}

/// <summary>
/// Configure serilog logger.
/// </summary>
private void ConfigureLogger()
{
string fullPath = Path.Combine(_folderPath, LogFileName);
Log.Logger = new LoggerConfiguration().WriteTo
.File($"{fullPath}_.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Message:lj}{NewLine}", rollingInterval: RollingInterval.Hour)
.CreateLogger();
}

/// <summary>
/// Retreive the name of the latest updated log file.
/// </summary>
/// <returns>Name of most recently updated log file.</returns>
private string GetLatestLogFileName()
{
string logPattern = $"{LogFileName}_*.txt";

string latestLogFile = Directory.GetFiles(_folderPath, logPattern)
.OrderByDescending(File.GetCreationTime)
.FirstOrDefault();

return latestLogFile;
}

/// <summary>
Expand All @@ -161,7 +207,7 @@ private void HandleMessageReceive(byte[] message)

if (IsLogging)
{
File.AppendAllText(LogFilePath, $"{ReceivedMessage}\n");
Log.Information(ReceivedMessage);
}
}

Expand Down
2 changes: 2 additions & 0 deletions ZeroMQSubscriber/ZeroMQSubscriber.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
<PackageReference Include="NetMQ" Version="4.0.1.13" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 90a66d9

Please sign in to comment.