Skip to content

Commit

Permalink
Simple MVC Core example with 2 log files #49
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyMF committed Feb 10, 2023
1 parent 8c243b0 commit 400d536
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions examples/Examples.sln
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs
Original file line number Diff line number Diff line change
@@ -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<LoggingDemoController> Logger;

public LoggingDemoController(ILogger<LoggingDemoController> 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();
}

}
}
26 changes: 26 additions & 0 deletions examples/TwoLogFilesMvc/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>()
.Build();

host.Run();
}
}
}
26 changes: 26 additions & 0 deletions examples/TwoLogFilesMvc/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
76 changes: 76 additions & 0 deletions examples/TwoLogFilesMvc/Startup.cs
Original file line number Diff line number Diff line change
@@ -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<FileLoggerOptions> 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}");
});
}
}
}
12 changes: 12 additions & 0 deletions examples/TwoLogFilesMvc/TwoLogFilesMvc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NReco.Logging.File" Version="1.1.5" />
</ItemGroup>


</Project>
30 changes: 30 additions & 0 deletions examples/TwoLogFilesMvc/Views/LoggingDemo/DemoPage.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@model System.Collections.IDictionary

<h1>
NReco.File.Logging demo
<small style="color:silver;">lightweight logging to files</small>
</h1>

<form action="@Url.Action("LogMessage", "LoggingDemo")" target="submitResult" method="post">

<table border="0">
<tr>
<th>LogLevel</th>
<td>
<select name="logLevel">
<option value="Debug">Debug</option>
<option value="Information">Information</option>
<option value="Warning">Warning</option>
</select>
</td>
</tr>
<tr>
<th>Message</th>
<td><input type="text" name="msg" value="This is a log message!"/></td>
</tr>
</table>

<button type="submit">Add</button>
</form>

<iframe name="submitResult" style="display:none;"></iframe>
25 changes: 25 additions & 0 deletions examples/TwoLogFilesMvc/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8" />
<title>NReco.Logging.File Demo</title>

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>

<style>
body {
font-family:Arial;
}
</style>

</head>
<body>

<div class="container">
@RenderBody()
<hr/>
</div>

@RenderSection("scripts", required: false)
</body>
</html>
3 changes: 3 additions & 0 deletions examples/TwoLogFilesMvc/Views/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
19 changes: 19 additions & 0 deletions examples/TwoLogFilesMvc/appsettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 400d536

Please sign in to comment.