From 400d536fae7d62781cf23a993b8e2e5489e6cf53 Mon Sep 17 00:00:00 2001 From: Vitalii Fedorchenko Date: Fri, 10 Feb 2023 13:31:25 +0200 Subject: [PATCH] Simple MVC Core example with 2 log files #49 --- README.md | 2 +- examples/Examples.sln | 25 ++++++ .../Controllers/LoggingDemoController.cs | 30 ++++++++ examples/TwoLogFilesMvc/Program.cs | 26 +++++++ .../Properties/launchSettings.json | 26 +++++++ examples/TwoLogFilesMvc/Startup.cs | 76 +++++++++++++++++++ examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj | 12 +++ .../Views/LoggingDemo/DemoPage.cshtml | 30 ++++++++ .../Views/Shared/_Layout.cshtml | 25 ++++++ .../TwoLogFilesMvc/Views/_ViewStart.cshtml | 3 + examples/TwoLogFilesMvc/appsettings.json | 19 +++++ 11 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 examples/Examples.sln create mode 100644 examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs create mode 100644 examples/TwoLogFilesMvc/Program.cs create mode 100644 examples/TwoLogFilesMvc/Properties/launchSettings.json create mode 100644 examples/TwoLogFilesMvc/Startup.cs create mode 100644 examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj create mode 100644 examples/TwoLogFilesMvc/Views/LoggingDemo/DemoPage.cshtml create mode 100644 examples/TwoLogFilesMvc/Views/Shared/_Layout.cshtml create mode 100644 examples/TwoLogFilesMvc/Views/_ViewStart.cshtml create mode 100644 examples/TwoLogFilesMvc/appsettings.json diff --git a/README.md b/README.md index bf003b0..7854793 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,6 @@ A new file name is applied in the same way as when it comes from the initial `Fi ## License -Copyright 2017-2022 Vitaliy Fedorchenko and contributors +Copyright 2017-2023 Vitaliy Fedorchenko and contributors Distributed under the MIT license diff --git a/examples/Examples.sln b/examples/Examples.sln new file mode 100644 index 0000000..50cf9e9 --- /dev/null +++ b/examples/Examples.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32510.428 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwoLogFilesMvc", "TwoLogFilesMvc\TwoLogFilesMvc.csproj", "{DE205ED8-D951-4CFF-9540-C5CE72E4B5AC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DE205ED8-D951-4CFF-9540-C5CE72E4B5AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE205ED8-D951-4CFF-9540-C5CE72E4B5AC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE205ED8-D951-4CFF-9540-C5CE72E4B5AC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE205ED8-D951-4CFF-9540-C5CE72E4B5AC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {09C478E3-6E43-4A9D-9EA3-9DEAEBEC22E2} + EndGlobalSection +EndGlobal diff --git a/examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs b/examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs new file mode 100644 index 0000000..820adf6 --- /dev/null +++ b/examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.IO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + + +namespace TwoLogFilesMvc { + + public class LoggingDemoController : Controller { + + ILogger Logger; + + public LoggingDemoController(ILogger logger) { + Logger = logger; + } + + public IActionResult DemoPage() { + return View(); + } + + public IActionResult LogMessage(string logLevel, string msg) { + Logger.Log( (LogLevel)Enum.Parse(typeof(LogLevel), logLevel, true), msg); + return Ok(); + } + + } +} diff --git a/examples/TwoLogFilesMvc/Program.cs b/examples/TwoLogFilesMvc/Program.cs new file mode 100644 index 0000000..272b77b --- /dev/null +++ b/examples/TwoLogFilesMvc/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Builder; + +namespace TwoLogFilesMvc +{ + public class Program + { + public static void Main(string[] args) + { + var host = new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseIIS() + .UseStartup() + .Build(); + + host.Run(); + } + } +} diff --git a/examples/TwoLogFilesMvc/Properties/launchSettings.json b/examples/TwoLogFilesMvc/Properties/launchSettings.json new file mode 100644 index 0000000..c403df7 --- /dev/null +++ b/examples/TwoLogFilesMvc/Properties/launchSettings.json @@ -0,0 +1,26 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49259/" + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "TwoLogFilesMvc": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://0.0.0.0:5000" + } + } +} \ No newline at end of file diff --git a/examples/TwoLogFilesMvc/Startup.cs b/examples/TwoLogFilesMvc/Startup.cs new file mode 100644 index 0000000..9174293 --- /dev/null +++ b/examples/TwoLogFilesMvc/Startup.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.IO; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NReco.Logging.File; + +namespace TwoLogFilesMvc +{ + public class Startup + { + IWebHostEnvironment HostingEnv; + + public Startup(IWebHostEnvironment env) + { + HostingEnv = env; + var builder = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); + + builder.AddEnvironmentVariables(); + Configuration = builder.Build(); + } + + public IConfigurationRoot Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container + public void ConfigureServices(IServiceCollection services) + { + services.AddLogging(loggingBuilder => { + var loggingSection = Configuration.GetSection("Logging"); + loggingBuilder.AddConfiguration(loggingSection); + loggingBuilder.AddConsole(); + + Action resolveRelativeLoggingFilePath = (fileOpts) => { + fileOpts.FormatLogFileName = fName => { + return Path.IsPathRooted(fName) ? fName : Path.Combine(HostingEnv.ContentRootPath, fName); + }; + }; + + loggingBuilder.AddFile(loggingSection, resolveRelativeLoggingFilePath); + loggingBuilder.AddFile(Configuration.GetSection("LoggingFileTwo"), resolveRelativeLoggingFilePath); + + // alternatively, you can configure 2nd file logger (or both) in the code: + /*loggingBuilder.AddFile("logs/app_debug.log", (fileOpts) => { + fileOpts.MinLevel = LogLevel.Debug; + resolveRelativeLoggingFilePath(fileOpts); + });*/ + + }); + + services.AddMvc(options => { + options.EnableEndpointRouting = false; + }); + + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + { + app.UseDefaultFiles(); + app.UseStaticFiles(); + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller=LoggingDemo}/{action=DemoPage}"); + }); + } + } +} diff --git a/examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj b/examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj new file mode 100644 index 0000000..a47dbd0 --- /dev/null +++ b/examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/examples/TwoLogFilesMvc/Views/LoggingDemo/DemoPage.cshtml b/examples/TwoLogFilesMvc/Views/LoggingDemo/DemoPage.cshtml new file mode 100644 index 0000000..641a414 --- /dev/null +++ b/examples/TwoLogFilesMvc/Views/LoggingDemo/DemoPage.cshtml @@ -0,0 +1,30 @@ +@model System.Collections.IDictionary + +

+ NReco.File.Logging demo + lightweight logging to files +

+ +
+ + + + + + + + + + +
LogLevel + +
Message
+ + +
+ + diff --git a/examples/TwoLogFilesMvc/Views/Shared/_Layout.cshtml b/examples/TwoLogFilesMvc/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000..a17a499 --- /dev/null +++ b/examples/TwoLogFilesMvc/Views/Shared/_Layout.cshtml @@ -0,0 +1,25 @@ + + + + + NReco.Logging.File Demo + + + + + + + + +
+ @RenderBody() +
+
+ + @RenderSection("scripts", required: false) + + diff --git a/examples/TwoLogFilesMvc/Views/_ViewStart.cshtml b/examples/TwoLogFilesMvc/Views/_ViewStart.cshtml new file mode 100644 index 0000000..efda124 --- /dev/null +++ b/examples/TwoLogFilesMvc/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "~/Views/Shared/_Layout.cshtml"; +} \ No newline at end of file diff --git a/examples/TwoLogFilesMvc/appsettings.json b/examples/TwoLogFilesMvc/appsettings.json new file mode 100644 index 0000000..c054573 --- /dev/null +++ b/examples/TwoLogFilesMvc/appsettings.json @@ -0,0 +1,19 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Warning", + "Microsoft": "Warning" + }, + "File": { + "Path": "logs/app.log", + "MinLevel": "Information" + } + }, + "LoggingFileTwo": { + "File": { + "Path": "logs/app_debug.log", + "MinLevel": "Debug" + } + } +}