Boost Your .NET Projects: Enhancing .NET Logging with LoggingHelper in Spargine

Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.

Get Spargine

You can access the source code and NuGet packages here:

Introducing the LoggingHelper

The LoggingHelper in DotNetTips.Spargine.Core provides a set of utility methods designed to streamline logging in .NET applications. It offers functionality to log system and application details while also capturing and logging all domain exceptions. Notably, it can handle exceptions across different assemblies, including those thrown by the LoggableException type in Spargine.

Effective logging of errors, events, and system information is critical for maintaining application reliability. With LoggingHelper, integrating robust logging into your application becomes effortless.

Key Features and Methods

Application and System Logging

  • LogApplicationInformation(ILogger logger)
    Logs essential application details as key-value pairs, including the company, version, copyright, product name, file version, and title.
  • LogComputerInformation(ILogger logger)
    Logs detailed system information, such as the operating system, architecture, memory usage, and more.

Exception Handling and Logging

  • LogAppDomainEvents(ILogger logger)
    Captures and logs key application domain events, including assembly load, assembly resolve, domain unload, process exit, and type resolution.
  • LogAppDomainFirstChanceException(ILogger logger)
    Enables logging of first-chance exceptions within the application domain.
  • LogAppDomainUnhandledException(ILogger logger)
    Logs unhandled exceptions as critical errors within the application domain.
  • RetrieveAllExceptionMessages(Exception exception)
    Extracts and returns all messages from an exception, including those from inner exceptions.
  • RetrieveAllExceptions(Exception exception)
    Retrieves all exceptions from a given exception, including inner exceptions.

Real-World Usage of Logging in Spargine Dev Tool

Logging is an essential component of modern software development, providing insights into application behavior, debugging, and system monitoring. In my development workflow, I frequently utilize methods such as LogAppDomainEvents and LogAppDomainUnhandledException, which are integral to the Spargine Dev Tool—a global development tool designed to enhance .NET development workflows by simplifying logging and diagnostics.

Example Log Output

Below are sample logs generated by LoggingHelper, showcasing the detailed application and system information it captures.

Application Information

These logs provide metadata about the running application, such as its configuration, memory usage, and version details:

2025-02-26 14:15:10.786 -06:00 [INF] AppInfo:Company - McCarter Consulting - dotNetTips.com
2025-02-26 14:15:10.827 -06:00 [INF] AppInfo:Configuration - Release
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:Copyright - David McCarter
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:Description - Tool to help Microsoft .NET developers with common and important tasks.
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:EqualityContract - DotNetTips.Spargine.Core.AppInfo
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:FileVersion - 2025.8.2.23
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:MemoryAllocated - 184264
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:Product - dotNetTips.Spargine.Dev.Tool
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:ThreadAllocatedBytes - 179600
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:Title - dotNetTips.Spargine.Dev.Tool
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:TotalAllocatedBytes - 183904
2025-02-26 14:15:10.828 -06:00 [INF] AppInfo:Version - 2025.8.2.23

Computer Information

System details such as OS version, architecture, memory usage, and network status are logged, making it easier to diagnose performance or environmental issues.

2025-02-27 14:29:20.976 -06:00 [INF] ComputerInfo:ComputerCulture - eng
2025-02-27 14:29:20.979 -06:00 [INF] ComputerInfo:ComputerUICulture - eng
2025-02-27 14:29:20.980 -06:00 [INF] ComputerInfo:CurrentManagedTreadId - 1
2025-02-27 14:29:20.981 -06:00 [INF] ComputerInfo:CurrentStackTrace -    at System.Environment.get_StackTrace()
   at DotNetTips.Spargine.Core.ComputerInfo..ctor()
   at DotNetTips.Spargine.Core.Logging.LoggingHelper.LogComputerInformation(ILogger logger)
   at DotNetTips.Spargine.Dev.Tool.Program.Main(String[] args) in D:\src\GitHub\Spargine.Global.Dev.Tool\source\Program.cs:line 125
2025-02-27 14:29:20.983 -06:00 [INF] ComputerInfo:CurrentSystemTickCount - 183582812
2025-02-27 14:29:20.984 -06:00 [INF] ComputerInfo:CurrentWorkingDirectory - D:\src\GitHub\Spargine.Global.Dev.Tool\source\bin\Debug\net8.0
2025-02-27 14:29:20.986 -06:00 [INF] ComputerInfo:FrameworkDescription - .NET 8.0.13
2025-02-27 14:29:20.987 -06:00 [INF] ComputerInfo:FrameworkVersion - 8.0.13
2025-02-27 14:29:20.989 -06:00 [INF] ComputerInfo:HasShutdownStarted - False
2025-02-27 14:29:20.990 -06:00 [INF] ComputerInfo:IPAddress - 192.168.5.555
2025-02-27 14:29:20.992 -06:00 [INF] ComputerInfo:Is64BitOperatingSystem - True
2025-02-27 14:29:20.993 -06:00 [INF] ComputerInfo:Is64BitProcess - True
2025-02-27 14:29:20.995 -06:00 [INF] ComputerInfo:IsNetworkAvailable - True
2025-02-27 14:29:20.996 -06:00 [INF] ComputerInfo:IsUserInteractive - True
2025-02-27 14:29:21.000 -06:00 [INF] ComputerInfo:MachineName - DESKTOP-DOTNETDAVE
2025-02-27 14:29:21.001 -06:00 [INF] ComputerInfo:OSArchitecture - X64
2025-02-27 14:29:21.003 -06:00 [INF] ComputerInfo:OSDescription - Microsoft Windows 10.0.19045
2025-02-27 14:29:21.004 -06:00 [INF] ComputerInfo:OSMemoryPageSize - 4096
2025-02-27 14:29:21.006 -06:00 [INF] ComputerInfo: PhysicalMemoryInUse - 46637056
2025-02-27 14:29:21.008 -06:00 [INF] ComputerInfo:ProcessArchitecture - X64
2025-02-27 14:29:21.009 -06:00 [INF] ComputerInfo:ProcessorCount - 12
2025-02-27 14:29:21.011 -06:00 [INF] ComputerInfo:SystemDirectory - C:\WINDOWS\system32
2025-02-27 14:29:21.012 -06:00 [INF] ComputerInfo:SystemPageSize - 4096
2025-02-27 14:29:21.014 -06:00 [INF] ComputerInfo:TickCount - 183582843
2025-02-27 14:29:21.015 -06:00 [INF] ComputerInfo:TickCount64 - 183582843
2025-02-27 14:29:21.016 -06:00 [INF] ComputerInfo:Uptime - 2.02:59:42.8750000
2025-02-27 14:29:21.018 -06:00 [INF] ComputerInfo:UserDomainName - DESKTOP-DOTNETDAVE
2025-02-27 14:29:21.019 -06:00 [INF] ComputerInfo:UserName – DOTNETDAVE

First Chance Exception Logging

This log captures a first chance exception, which helps in diagnosing missing dependencies or misconfigurations.

2025-02-27 14:29:49.700 -06:00 [ERR] FirstChanceException in dotNetTips.Spargine.Dev.Tool:  Could not load file or assembly 'dotNetTips.Spargine.Dev.Tool.XmlSerializers, Version=2025.8.2.23, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL'. The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'dotNetTips.Spargine.Dev.Tool.XmlSerializers, Version=2025.8.2.23, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL'. The system cannot find the file specified.
File name: 'dotNetTips.Spargine.Dev.Tool.XmlSerializers, Version=2025.8.2.23, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL'
   at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)

Summary

The Spargine LoggingHelper provides developers with a powerful logging mechanism that captures detailed system and application insights. By leveraging LoggingHelper, developers can proactively monitor application health, diagnose issues faster, and improve overall software reliability. The example logs above illustrate just how valuable structured logging is in real-world development scenarios.

Get Involved!

The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:

  • Submitting pull requests
  • Reporting issues
  • Suggesting new features

Your input is invaluable in making Spargine an even more powerful tool for the .NET community.

If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!

Thank you, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.