TPJ.Logging
2.1.1
See the version list below for details.
dotnet add package TPJ.Logging --version 2.1.1
NuGet\Install-Package TPJ.Logging -Version 2.1.1
<PackageReference Include="TPJ.Logging" Version="2.1.1" />
paket add TPJ.Logging --version 2.1.1
#r "nuget: TPJ.Logging, 2.1.1"
// Install TPJ.Logging as a Cake Addin #addin nuget:?package=TPJ.Logging&version=2.1.1 // Install TPJ.Logging as a Cake Tool #tool nuget:?package=TPJ.Logging&version=2.1.1
TPJ Logging library
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net451 is compatible. 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. |
-
.NETFramework 4.5.1
- Microsoft.Extensions.Configuration.Abstractions (>= 1.0.0)
- Microsoft.Extensions.Options (>= 1.0.0)
-
.NETFramework 4.5.2
- Microsoft.Extensions.Configuration.Abstractions (>= 1.0.0)
- Microsoft.Extensions.Options (>= 1.0.0)
-
.NETFramework 4.6
- Microsoft.Extensions.Configuration.Abstractions (>= 1.0.0)
- Microsoft.Extensions.Options (>= 1.0.0)
-
.NETFramework 4.6.1
- Microsoft.Extensions.Configuration.Abstractions (>= 1.0.0)
- Microsoft.Extensions.Options (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
ASP.Net Core Website Set up
Create appsettings.json file and add the following
{
"TPJ": {
"Logging": {
"ApplicationName": "",
"ErrorLogType": "",
"LogFileDirectory": "",
"ErrorEmails": ""
},
"Email": {
"SmtpClient": "",
"SmtpUser": "",
"SmtpPassword": "",
"EmailFrom": "",
"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
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
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)
services.Configure<TPJ.Logging.Models.LoggerProperties>(options =>
{
options.ApplicationName = Configuration["TPJ:Logging:ApplicationName"];
options.ErrorEmailList = Configuration["TPJ:Logging:ErrorEmails"].Split(';').ToList();
options.LogFileDirectory = Configuration["TPJ:Logging:LogFileDirectory"];
options.ErrorLogType = TPJ.Logging.Helper.GetLogType(Configuration["TPJ:Logging:ErrorLogType"]);
options.LogEnvironment = TPJ.Logging.Helper.GetLogLevelEnvironment(Configuration["TPJ:Logging:LogEnvironment"]);
options.SmtpClient = Configuration["TPJ:Email:SmtpClient"];
options.EmailFrom = Configuration["TPJ:Email:EmailFrom"];
options.SmtpUser = Configuration["TPJ:Email:SmtpUser"];
options.SmtpPassword = Configuration["TPJ:Email:SmtpPassword"];
});
Top Tip - you can use the log environment using Configuration["ASPNETCORE_ENVIRONMENT"] instead of using your json file. To do this.
public void ConfigureServices(IServiceCollection services, IHostingEnvironment env)
{
services.Configure<TPJ.Logging.Models.LoggerProperties>(options =>
{
options.ApplicationName = Configuration["TPJ:Logging:ApplicationName"];
options.ErrorEmailList = Configuration["TPJ:Logging:ErrorEmails"].Split(';').ToList();
options.LogFileDirectory = Configuration["TPJ:Logging:LogFileDirectory"];
options.ErrorLogType = TPJ.Logging.Helper.GetLogType(Configuration["TPJ:Logging:ErrorLogType"]);
options.LogEnvironment = TPJ.Logging.Helper.GetLogLevelEnvironment(Configuration["ASPNETCORE_ENVIRONMENT"]);
options.SmtpClient = Configuration["TPJ:Email:SmtpClient"];
options.EmailFrom = Configuration["TPJ:Email:EmailFrom"];
options.SmtpUser = Configuration["TPJ:Email:SmtpUser"];
options.SmtpPassword = Configuration["TPJ:Email:SmtpPassword"];
});
}
services.AddTransient<TPJ.Logging.Interfaces.ILogger, TPJ.Logging.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);
return View();
}
catch (Exception e)
{
_logger.Error(System.Reflection.MethodBase.GetCurrentMethod(), e, Enums.LogEnvironment.Production);
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.
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 TPJ.Logging.Interfaces.ILogger _logger;
Then add set up method
private static void SetUp()
{
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
_logger = new TPJ.Logging.Logger(new TPJ.Logging.Models.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);
_logger.Error(System.Reflection.MethodBase.GetCurrentMethod(), new Exception("Test"), Enums.LogEnvironment.Staging);
}