TPJ.Logging 3.0.0

TPJ Logging library - Easily Log errors into a log file and/or e-mail. Simple, light weight, easy to setup!

There is a newer version of this package available.
See the version list below for details.
Install-Package TPJ.Logging -Version 3.0.0
dotnet add package TPJ.Logging --version 3.0.0
<PackageReference Include="TPJ.Logging" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TPJ.Logging --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Release Notes

V3.0.0 only supports .netstandard2.0, which works in .net4.6.1 + / .netcore2.0 +. If you are using .net4.5.2 / .net 4.6 you can use TPJ.Logging V2.X.X.

ASP.Net Core Website / WebAPI Set up using both E-mail and Log file.

Within  appsettings.json and add the following

 "TPJ": {
   "Logging": {
     "ApplicationName": "",
       "LogType": "",
       "LogFileDirectory": "",
         "To": "",
         "From": "",
         "SmtpClient": "",
         "SmtpUser": "",
         "SmtpPassword": "",
         "Port": "",
         "EnableSSL": ""

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
             1) Email - Errors are sent via e-mail only (as per rest of the config settings)
             2) LogFile - Errors are logged in a txt file (named – ‘[{Environment}] {Application Name} Error Log.txt’)
             3) 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 ";"

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 - (Not required) send e-mail using the given user name and password

SmtpPassword - (Not required) send e-mail using the given user name and password

Port - (Not required) port to send from

EnableSSL - (Not required) enable SSL when sending the e-mail

Example appsettings.json setup using Gmail to send error e-mails –
 "TPJ": {
   "Logging": {
     "ApplicationName": "Test Application",
       "LogType": "EmailLogFile",
       "LogFileDirectory": "C:\Logging",
         "To": "",
         "From": " ",
         "SmtpClient": "",
         "SmtpUser": "",
         "SmtpPassword": "testPassword",
         "Port": "587",
         "EnableSSL": "true"

Once appsettings.json is done open StartUp.cs file and go to ConfigureServices

var logSettings = new TPJ.Logging.Models.ErrorLogSettings(Configuration);
services.Configure< TPJ.Logging.Models.ErrorLogSettings>(options =>
   options.ApplicationName = logSettings.ApplicationName;
   options.LogType = loggSettings. LogType;
   options.LogFileDirectory = loggSettings.LogFileDirectory;
   options.EmailTo = loggSettings.EmailTo;
   options.EmailFrom = loggSettings.EmailFrom;
   options.SmtpClient = loggSettings.SmtpClient;
   options.SmtpUser = loggSettings.SmtpUser;

   options.SmtpPassword = loggSettings.SmtpPassword;
   options.Port = loggSettings.Port;
   options.EnableSSL= loggSettings.EnableSSL;
services.AddSingleton< TPJ.Logging.IErrorLogger, TPJ.Logging.ErrorLogger>();

Then using DI within core you can call IErrorLogger like so

private readonly TPJ.Logging.IErrorLogger _errorLogger;

public HomeController(TPJ.Logging.IErrorLogger logger)
 _errorLogger = errorLogger;

Then within an IActionResult you might have this

public IActionResult About(Divide divide)
    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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.1.2 537 8/19/2017
3.1.1 422 8/16/2017
3.0.0 405 8/15/2017 360 8/14/2017
2.5.1 416 7/12/2017
2.5.0 508 3/21/2017
2.2.3 573 10/28/2016
2.1.1 581 8/26/2016
1.1.3 704 4/19/2016