-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple MVC Core example with 2 log files #49
- Loading branch information
Showing
11 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
examples/TwoLogFilesMvc/Controllers/LoggingDemoController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@{ | ||
Layout = "~/Views/Shared/_Layout.cshtml"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |