In this post I will be talking about application logging. Performing logging actions in the background of an application is very important. This will help a developer or anyone supporting the application by showing important information about what is happening with the application. For instance, you might have an application that is deployed to all of the hosts in your domain and Mike from Accounting might be having issues with the application. If logging is setup in an appropriate way, you might be able to navigate to Mike’s filesystem and access the application logs to see what was happening while he was using it. This is just a quick and simple example of why you would use logging in your application but there are many others.
Let’s see how to implement logging quickly and easily into an example application. For our example, I will be creating a .NET 6 Console application. We will be using a .NET Library called Serilog. You can find out more about Serilog here. The program itself will do nothing special; We will just dump some text into the terminal. But we will be logging some information in the background.
Creating the Project
First, we will create a new console project and add the necessary Serilog packages to the project. To create the project, run the following:
PS> dotnet new console -o SystemLogging
“SystemLogging” refers to the name of the project. From here we will navigate into the project folder and add the Serilog package as well as the File Sink package:
PS> dotnet add package Serilog
PS> dotnet add package Serilog.Sinks.File
With these two libraries we will be able to use Serilog to write logs to a file.
Configuring Serilog
Now we will fire up any flavor of Visual Studio (I am using Visual Studio Code) and open the Program.cs
file. In this file I will delete the console template’s code and add Serilog to the file:
using Serilog;
For the configuration I will create a new logger and have it write to a file that rolls over every day. This will make it easier to narrow down where to start looking if any user is having an issue with the application.
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/.log", rollingInterval: RollingInterval.Day)
.CreateLogger();
Now we can start logging.
NOTE: In the configuration, I have the file with a folder. This will automatically create the folder and file.
Logging
I have created a class that has a couple of properties: HostName
and IPv4Address
. These will represent the host’s hostname and IP address (I will post the code for the class at the end of this post). The program will get these two properties and log them as informational logs to our logging file and then continue running. In this example I only log informational logs, but you can use error and warning logs as well as others.
using (SystemInfo localhost = new())
{
Log.Information("Starting SystemLogging...");
Log.Information($" Hostname: {localhost.HostName}");
Log.Information($"IPv4 Address: {localhost.IPv4Address}");
}
WriteLine("Application Running...");
WriteLine("Finished!");
Log.Information("Application Closing. Exit code 0");
Log.CloseAndFlush();
The Logs
After running the program, we can now see that there is a newly created file that contains all of the logs from when we ran the program.
2022-10-28 16:42:29.557 -07:00 [INF] Starting SystemLogging...
2022-10-28 16:42:29.591 -07:00 [INF] Hostname: SANFORD-PC
2022-10-28 16:42:29.591 -07:00 [INF] IPv4 Address: 172.17.224.1
2022-10-28 16:42:29.593 -07:00 [INF] Application Closing. Exit code 0
Again, this is just a simple example of logging. Really you can log just about anything that you would want or need to. Happy logging!
Project Files
Program.cs
#region Usings
using Serilog;
using SystemLogging.System;
using static System.Console;
#endregion
#region Logging Configuration
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/.log", rollingInterval: RollingInterval.Day)
.CreateLogger();
#endregion
#region Main
using (SystemInfo localhost = new())
{
Log.Information("Starting SystemLogging...");
Log.Information($" Hostname: {localhost.HostName}");
Log.Information($"IPv4 Address: {localhost.IPv4Address}");
}
WriteLine("Application Running...");
WriteLine("Finished!");
Log.Information("Application Closing. Exit code 0");
Log.CloseAndFlush();
#endregion
System.SystemInfo.cs
using System.Net;
namespace SystemLogging.System;
public class SystemInfo : IDisposable
{
// Properties
public string HostName { get; private set; }
public string IPv4Address { get; private set; }
// Constructor
public SystemInfo()
{
this.HostName = GetHostName();
this.IPv4Address = GetIPv4Address(this.HostName);
}
// Methods
private string GetHostName()
{
return Dns.GetHostName();
}
private string GetIPv4Address(string _hostName)
{
IPHostEntry _hostEntry = Dns.GetHostEntry(_hostName);
IPAddress[] _ipAddresses = _hostEntry.AddressList;
return _ipAddresses[_ipAddresses.Length - 1].ToString();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}