TPJ.Logging
4.0.0
dotnet add package TPJ.Logging --version 4.0.0
NuGet\Install-Package TPJ.Logging -Version 4.0.0
<PackageReference Include="TPJ.Logging" Version="4.0.0" />
paket add TPJ.Logging --version 4.0.0
#r "nuget: TPJ.Logging, 4.0.0"
// Install TPJ.Logging as a Cake Addin #addin nuget:?package=TPJ.Logging&version=4.0.0 // Install TPJ.Logging as a Cake Tool #tool nuget:?package=TPJ.Logging&version=4.0.0
TPJ.Logging
Simple error logging library that can send emails and/or log to a txt file
Setup
appsettings.json
ApplicationName
� (Required) Name of the application used on the log file names and e-mails
ErrorLogType
(Required) - There are three types of error log types:
- Email - Errors are sent via e-mail only (as per rest of the config settings)
- LogFile - Errors are logged in a txt file (named �
[{Environment}] {Application Name} Error Log.txt
) - EmailLogFile - Does both Email and LogFile
LogFileDirectory
(Required for log file logging) - The location at which the log / error file will be placed
To
(Required for e-mail logging) - Error e-mails sent to; Can be a list split by ;
E.G "Test@test.com;Test2@test.com"
From
(Required for e-mail logging) - E-mails are sent from this account
SmtpClient
(Required for e-mail logging) - SMTP server which e-mails will be sent from
SmtpUser
- Send e-mail using the given user name and password
SmtpPassword
- Send e-mail using the given user name and password
Port
- Port to send from
EnableSSL
- Enable SSL when sending the e-mail
Example
Within appsettings.json
and add (add your own settings):
"TPJ": {
"Logging": {
"ApplicationName": "Test App",
"LogType": "EmailLogFile",
"LogFileDirectory": "C:\\Test",
"Email": {
"From": "test-app@test.com",
"SmtpClient": "smtp.gmail.com",
"SmtpUser": "abc",
"SmtpPassword": "xyz",
"EnableSSL": true,
"Port": 587
}
}
}
Within appsettings.{environment}.json
add:
"TPJ": {
"Logging": {
"Email": {
"To": "example@test.com"
}
}
}
ConfigureServices
To add logging simply add TPJ logging to your DI:
services.AddTPJLogging();
Example Error Log
Check out the git repo code for example setup for a console app and API.
private readonly TPJ.Logging.ILogger _logger;
public HomeController(TPJ.Logging.ILogger logger)
{
_logger = logger;
}
Then within an IActionResult
you might have this
public IActionResult About(Divide divide)
{
try
{
var divideByZero = divide.ValueOne / divide.ValueTwo;
return View();
}
catch (Exception e)
{
_logger.Log(System.Reflection.MethodBase.GetCurrentMethod(), e, divide);
return RedirectToAction(nameof(Error));
}
}
This will then log the error with the current method information, the exception details and the details of the object �divide� � it will log nested classes within objects.
GDPR Sensitive Data
Any data that is personal / sensitive such as e-mail, address, passwords should not be logged or sent over e-mail. To hide this information you add [Sensitive]
data attriribute to the property that contains the sensitive information.
[Sensitive]
public string Password { get; set; }
This will then remove the data when logging and place ##Redacted##
instead.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
2023 update! V4.0.0 now runs on .NET 7 see github for more details