TPJ.Logging 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package TPJ.Logging --version 1.1.3
NuGet\Install-Package TPJ.Logging -Version 1.1.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="1.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TPJ.Logging --version 1.1.3
#r "nuget: TPJ.Logging, 1.1.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=1.1.3

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

TPJ.Logging Class Library

Product Compatible and additional computed target framework versions.
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
DNX dnx451 is compatible. 
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 289 1/28/2023
3.1.2 1,160 8/19/2017
3.1.1 996 8/16/2017
3.0.0 922 8/15/2017
2.5.1.1 905 8/14/2017
2.5.1 954 7/12/2017
2.5.0 1,107 3/21/2017
2.2.3 1,204 10/28/2016
2.1.1 1,224 8/26/2016
1.1.3 1,619 4/19/2016

ASP.Net Core Website Set up

Create appsettings.json file and add the following

"LogSettings": {
   "ApplicationName": "",
   "ErrorLogType": "",
   "LogEnvironment": "",
   "LogFileDirectory": "",
   "ErrorEmails": ""
},
"EmailSettings": {
   "SmtpClient": "",
   "SmtpUser": "",
   "SmtpPassword": "",
   "EmailFrom": ""
}

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 "test" will log when the config is set to development and test but NOT live
             1) Development
             2) Test
             3) Live

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

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

EmailFrom - E-mails are sent from this account

ErrorEmails - List of all e-mail address which error e-mails will be sent to split using ; e.g test@test.com;test2@test.com;test3@test.com



For Websites -

Open StartUp.cs file and go to ConfigureServices

Add the following

services.Configure<LoggerProperties>(options =>
{
     options.ApplicationName = Configuration["LogSettings:ApplicationName"];
     options.SmtpClient = Configuration["LogSettings:SmtpClient"];
     options.EmailFrom = Configuration["LogSettings:EmailFrom"];
     options.ErrorEmailList = Configuration["LogSettings:ErrorEmails"].Split(';').ToList();
     options.LogFileDirectory = Configuration["LogSettings:LogFileDirectory"];
     options.ErrorLogType = Helper.GetLogType(Configuration["LogSettings:ErrorLogType"]);
     options.LogEnvironment = Helper.GetLogLevelEnvironment(Configuration["LogSettings:LogEnvironment"]);
});

services.AddTransient<TPJ.Logging.Interfaces.ILogger, Logger>();

Then using DI within asp.net core you can call ILogger like so

private readonly TPJ.Logging.Interfaces.ILogger _logger;

public HomeController(TPJ.Logging.Interfaces.ILogger logger)
{
   _logger = logger;
}

Then within an IActionResult you might have this

public IActionResult About()
{
 try
 {
   _logger.InformationLog("About Page Loaded", Enums.LogEnvironment.Development, true);

   return View();

 }
 catch (Exception e)
 {
   _logger.Error(System.Reflection.MethodBase.GetCurrentMethod(), e, Enums.LogEnvironment.Live, true);

   return RedirectToAction(nameof(Error));
 }
}

Here a log will be made each time the "about" page is requested but only logged when in development. Where as if it errors a error log will always be made. Both methods will fire and forget to improve loading times for the pages.


ASP.Net Core Console Application

This is how I do it. This may change and I'm sure there are different ways to do this. This is also just to get the logger to work. You may need to change things to get other packages to load.

Add to the top of the program class

public static ILogger _logger;

Then add set up method

private static void SetUp()
{
   var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");

   _logger = new Logger(new LoggerProperties(builder.Build()));
}

Then call the "SetUp" method from the Main method

public static void Main(string[] args)
{
   SetUp();
}

Then you can call the logger like so

public static void Main(string[] args)
{
   SetUp();

   _logger.InformationLog("Test Log", Enums.LogEnvironment.Development, false);

   _logger.Error(System.Reflection.MethodBase.GetCurrentMethod(), new Exception("Test"), Enums.LogEnvironment.Test, false);
}

With console application do not "fire and forget" as if the application ends and the log is not completed it will not log.