TPJ.Logging 2.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package TPJ.Logging --version 2.2.3
NuGet\Install-Package TPJ.Logging -Version 2.2.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TPJ.Logging" Version="2.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TPJ.Logging --version 2.2.3
#r "nuget: TPJ.Logging, 2.2.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TPJ.Logging as a Cake Addin
#addin nuget:?package=TPJ.Logging&version=2.2.3

// Install TPJ.Logging as a Cake Tool
#tool nuget:?package=TPJ.Logging&version=2.2.3

TPJ Logging library does both Error logging to e-mail and log file and audit logging to log file or database

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 291 1/28/2023
3.1.2 1,162 8/19/2017
3.1.1 998 8/16/2017
3.0.0 924 8/15/2017
2.5.1.1 907 8/14/2017
2.5.1 956 7/12/2017
2.5.0 1,109 3/21/2017
2.2.3 1,206 10/28/2016
2.1.1 1,226 8/26/2016
1.1.3 1,619 4/19/2016

ASP.Net Core full framework (4.5.2 4.6 4.6.1) Website Set up

Create appsettings.json file and add the following

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

ApplicationName - Name of the application used on the log file names and e-mails

ErrorLogType - 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 - {Application Name} Error Log.txt)
             3) EmailLogFile - Does both Email and LogFile

LogEnvironment - There are three environment types. logging will be done on all logs marked at or below the log environment type E.G logs marked at "Staging" will log when the config is set to development and staging but NOT production
             1) Development
             2) Staging
             3) Production

LogFileDirectory - the location at which the log / error file will be placed

To - Error e-mails sent to; Can be a list split by ';' E.G "Test@test.com;Test2@test.com"

From - E-mails are sent from this account

SmtpClient - 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


For Websites -

Open StartUp.cs file and go to ConfigureServices

Add the following (I do not need port or enable SSL so have not include them)

var logSettings = new TPJ.Logging.Models.ErrorLogSettings(Configuration);
           
services.Configure<TPJ.Logging.Models.ErrorLogSettings>(options =>
{
  options.ApplicationName = logSettings.ApplicationName;
  options.EmailFrom = logSettings.EmailFrom;
  options.EmailTo = logSettings.EmailTo;
  options.LogEnvironment = logSettings.LogEnvironment;
  options.SmtpClient = logSettings.SmtpClient;
});

Top Tip - If you dont put in a log environment it will default to use the Configuration["ASPNETCORE_ENVIRONMENT"]


services.AddSingleton<TPJ.Logging.IErrorLogger, TPJ.Logging.ErrorLogger>();

Then using DI within asp.net 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()
{
  try
  {
     return View();
  }
  catch (Exception e)
  {
     _logger.log(System.Reflection.MethodBase.GetCurrentMethod(), e, TPJ.Logging.Enums.LogEnvironment.Production);

     return RedirectToAction(nameof(Error));
  }
}