diff --git a/WorkFlow.API/Program.cs b/WorkFlow.API/Program.cs index 3f4af42..adcadf5 100644 --- a/WorkFlow.API/Program.cs +++ b/WorkFlow.API/Program.cs @@ -8,58 +8,74 @@ using DigitalData.Core.Application; using DigitalData.UserManager.Application.DTOs.User; using Microsoft.IdentityModel.Tokens; using WorkFlow.API.Models; -using System.Security.Claims; +using NLog; +using NLog.Web; -var builder = WebApplication.CreateBuilder(args); -var config = builder.Configuration; +var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); +logger.Info("Logging initialized."); -// Add services to the container. -var cnn_str = config.GetConnectionString("Default") ?? throw new ("Default connection string not found."); -builder.Services.AddDbContext(options => options.UseSqlServer(cnn_str).EnableDetailedErrors()); -builder.Services.AddWorkFlow().AddUserManager(); -builder.Services.AddCookieBasedLocalizer(); -builder.ConfigureBySection(); -builder.Services.AddDirectorySearchService(); -builder.Services.AddJWTService(user => new SecurityTokenDescriptor() +try { - Claims = user.ToClaimList().ToDictionary(claim => claim.Type, claim => claim.Value as object) -}); + var builder = WebApplication.CreateBuilder(args); + var config = builder.Configuration; -builder.Services.AddControllers(); + // Add NLogger + builder.Logging.ClearProviders(); + builder.Host.UseNLog(); -builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(options => + // Add services to the container. + var cnn_str = config.GetConnectionString("Default") ?? throw new("Default connection string not found."); + builder.Services.AddDbContext(options => options.UseSqlServer(cnn_str).EnableDetailedErrors()); + builder.Services.AddWorkFlow().AddUserManager(); + builder.Services.AddCookieBasedLocalizer(); + builder.ConfigureBySection(); + builder.Services.AddDirectorySearchService(); + builder.Services.AddJWTService(user => new SecurityTokenDescriptor() { - options.Cookie.HttpOnly = true; // Makes the cookie inaccessible to client-side scripts for security - options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; // Ensures cookies are sent over HTTPS only - options.Cookie.SameSite = SameSiteMode.Strict; // Protects against CSRF attacks by restricting how cookies are sent with requests from external sites - options.LoginPath = "/api/auth/login"; - options.LogoutPath = "/api/auth/logout"; - options.ExpireTimeSpan = TimeSpan.FromMinutes(60); // timeout. - options.SlidingExpiration = true; //refreshes the expiration time on each request. - options.Cookie.Name = "AuthSession"; + Claims = user.ToClaimList().ToDictionary(claim => claim.Type, claim => claim.Value as object) }); -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); + builder.Services.AddControllers(); -var app = builder.Build(); + builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(options => + { + options.Cookie.HttpOnly = true; // Makes the cookie inaccessible to client-side scripts for security + options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; // Ensures cookies are sent over HTTPS only + options.Cookie.SameSite = SameSiteMode.Strict; // Protects against CSRF attacks by restricting how cookies are sent with requests from external sites + options.LoginPath = "/api/auth/login"; + options.LogoutPath = "/api/auth/logout"; + options.ExpireTimeSpan = TimeSpan.FromMinutes(60); // timeout. + options.SlidingExpiration = true; //refreshes the expiration time on each request. + options.Cookie.Name = "AuthSession"; + }); -// Configure the HTTP request pipeline. -if (app.IsDevOrDiP() && app.Configuration.GetValue("EnableSwagger")) -{ - app.UseSwagger(); - app.UseSwaggerUI(); + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.IsDevOrDiP() && app.Configuration.GetValue("EnableSwagger")) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthentication(); + + app.UseAuthorization(); + + app.UseCookieBasedLocalizer("de-DE"); + + app.MapControllers(); + + app.Run(); } - -app.UseHttpsRedirection(); - -app.UseAuthentication(); - -app.UseAuthorization(); - -app.UseCookieBasedLocalizer("de-DE"); - -app.MapControllers(); - -app.Run(); \ No newline at end of file +catch (Exception ex) +{ + logger.Error(ex, "Stopped program because of exception."); + throw; +} \ No newline at end of file diff --git a/WorkFlow.API/WorkFlow.API.csproj b/WorkFlow.API/WorkFlow.API.csproj index f3987f2..047781d 100644 --- a/WorkFlow.API/WorkFlow.API.csproj +++ b/WorkFlow.API/WorkFlow.API.csproj @@ -8,6 +8,8 @@ + + diff --git a/WorkFlow.API/appsettings.json b/WorkFlow.API/appsettings.json index 89037e4..28ebabd 100644 --- a/WorkFlow.API/appsettings.json +++ b/WorkFlow.API/appsettings.json @@ -7,6 +7,49 @@ "Microsoft.AspNetCore": "Warning" } }, + "NLog": { + "throwConfigExceptions": true, + "variables": { + "logDirectory": "E:\\LogFiles\\Digital Data\\workFlow", + "logFileNamePrefix": "${shortdate}-ECM.EnvelopeGenerator.Web" + }, + "targets": { + "infoLogs": { + "type": "File", + "fileName": "${logDirectory}\\${logFileNamePrefix}-Info.log", + "maxArchiveDays": 30 + }, + "errorLogs": { + "type": "File", + "fileName": "${logDirectory}\\${logFileNamePrefix}-Error.log", + "maxArchiveDays": 30 + }, + "criticalLogs": { + "type": "File", + "fileName": "${logDirectory}\\${logFileNamePrefix}-Critical.log", + "maxArchiveDays": 30 + } + }, + // Trace, Debug, Info, Warn, Error and *Fatal* + "rules": [ + { + "logger": "*", + "minLevel": "Info", + "maxLevel": "Warn", + "writeTo": "infoLogs" + }, + { + "logger": "*", + "level": "Error", + "writeTo": "errorLogs" + }, + { + "logger": "*", + "level": "Fatal", + "writeTo": "criticalLogs" + } + ] + }, "AllowedHosts": "*", "ConnectionStrings": { "Default": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;"