Simple in use library for logging called meber methods, exceptions, properties with parameters and their values, and saving them in clear and transparent format.
Library was created for logging method calls, exceptions, property changes and additional data selected by the user from a solution, and saving the logs in a file with clear, transparent and readable format. Currently logger works only with .Net Framework.
- Parsing method and properties parameter values with their names, with using the minimum amount of code.
- Saving the parameters logs with easy to read format.
- Setup library configuration in separate log.config file.
- Synchronous calls of the library interface.
- Possibility to place log.config anywhere in the project.
- Creating singleton object of the logger.
- Logged data types:
- property name with value,
- called method name with property names and values,
- special information strings called by developer,
- exceptions.
- Configuration covers:
- log file path and name,
- use only in debug or not,
- deleting logs on application start,
- log in console and/or file,
- console display only INFO logs
To use the library you need to install it with NuGet. After installation you can create singleton of ParamsLogger
in your class and all other classes of your project:
public class YourViewModel
{
private static readonly ILogger _log = ParamsLogger.LogInstance.GetLogger(); //singleton
public YourViewModel()
{
//ctor
}
}
Somewhere inside of your project you need to create log.config file. Your log file path will be in relation to the assembly of the application. Setup example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add
key="debugOnly"
value="true"
/>
<add
key="logFile"
value="log.txt"
/>
<add
key="deleteLogs"
value="true"
/>
<add
key="fileLog"
value="true"
/>
<add
key="consoleLog"
value="true"
/>
<add
key="infoOnlyConsole"
value="true"
/>
</appSettings>
</configuration>
Where:
debugOnly
means that will save logs with debugger attached only, default value:true
,logFile
means where will save the logs, default value: is project main folder with file namelog.txt
,deleteLogs
is for deleting log file on application startup, default value:true
,fileLog
is a statement, that you want to save logs into log file, default value:true
,consoleLog
is a statement, that you want to display logs in console, default value:true
,infoOnlyConsole
ifconsoleLog
istrue
, you can decide to display only INFO logs, default value:true
.
- Logging called method with parameter names and values example:
public void ExecutePauseButton(bool paused)
{
_log.Called(paused); //log
if (paused == true)
{
_controlsService.GetStartedConfiguration();
}
else
{
_controlsService.GetPausedConfiguration();
}
}
Calling method with bool parameter gives sample output:
07:05:49.398|CALLED|ButtonsService|ExecutePauseButton((Boolean)paused=False)
- Logging property with its name and value example:
private bool _paused;
public bool Paused
{
get => _paused;
set
{
_paused = value;
_log.Prop(_paused);
}
}
Setting up propaerty value with bool parameter gives sample output:
07:05:49.323|PROP|BrowserViewModel|set_Paused(True)
- Logging special information string:
_log.Info("Paused");
It gives sample output:
07:05:52.44|INFO|BrowseService|Pause((Paused))
- Logging exceptions:
public async Task LoopCollectingAsync()
{
try
{
InputCorrection = false;
await GetNewRecordAsync();
await LoopCollectingAsync();
}
catch (Exception e)
{
_log.Error(e.Message);
}
}
When method catches exception, it gives sample output:
08:41:55.370|ERROR|BrowseService|LoopCollectingAsync((The operation was canceled.))
- Approaches:
- is called synchronously,
- logs are saved asynchronously,
- locking log object on creation,
- singleton pattern for logger class,
- facade used for hiding dependencies,
- factory pattern for creation of tyhe logger,
- logging unhandled fatal exceptions.
- Library is using:
- .Net Standard 2.0,
- System.Reflection,
- System.Configuration.ConfigurationManager.
30 Aug 2019