Skip to content

Commit d11b1c2

Browse files
committed
.NET Core Logging
.NET Core Logging And NLog Sample
1 parent 715128f commit d11b1c2

65 files changed

Lines changed: 23607 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

NETCoreLogging/NETCoreLogging.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{750DB4F6-4C5E-47F8-9BD9-D30181C64F39}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{83FED358-0394-485F-BB82-0C59D67701BC}"
9+
ProjectSection(SolutionItems) = preProject
10+
global.json = global.json
11+
EndProjectSection
12+
EndProject
13+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NETCoreLogging", "src\NETCoreLogging\NETCoreLogging.xproj", "{CB2F89A8-8E72-4084-936F-C2A0D27F161E}"
14+
EndProject
15+
Global
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|Any CPU = Debug|Any CPU
18+
Release|Any CPU = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{CB2F89A8-8E72-4084-936F-C2A0D27F161E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{CB2F89A8-8E72-4084-936F-C2A0D27F161E}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{CB2F89A8-8E72-4084-936F-C2A0D27F161E}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{CB2F89A8-8E72-4084-936F-C2A0D27F161E}.Release|Any CPU.Build.0 = Release|Any CPU
25+
EndGlobalSection
26+
GlobalSection(SolutionProperties) = preSolution
27+
HideSolutionNode = FALSE
28+
EndGlobalSection
29+
GlobalSection(NestedProjects) = preSolution
30+
{CB2F89A8-8E72-4084-936F-C2A0D27F161E} = {750DB4F6-4C5E-47F8-9BD9-D30181C64F39}
31+
EndGlobalSection
32+
EndGlobal

NETCoreLogging/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

NETCoreLogging/global.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"projects": [ "src", "test" ],
3+
"sdk": {
4+
"version": "1.0.0-preview1-002702"
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "wwwroot/lib"
3+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace NETCoreLogging.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
public IActionResult Index()
19+
{
20+
_logger.LogInformation("你访问了首页");
21+
_logger.LogWarning("警告信息");
22+
_logger.LogError("错误信息");
23+
return View();
24+
}
25+
26+
public IActionResult About()
27+
{
28+
ViewData["Message"] = "Your application description page.";
29+
30+
return View();
31+
}
32+
33+
public IActionResult Contact()
34+
{
35+
ViewData["Message"] = "Your contact page.";
36+
37+
return View();
38+
}
39+
40+
public IActionResult Error()
41+
{
42+
return View();
43+
}
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>cb2f89a8-8e72-4084-936f-c2a0d27f161e</ProjectGuid>
10+
<RootNamespace>NETCoreLogging</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<DnxInvisibleContent Include="bower.json" />
20+
<DnxInvisibleContent Include=".bowerrc" />
21+
<DnxInvisibleContent Include="package.json" />
22+
<DnxInvisibleContent Include=".npmrc" />
23+
</ItemGroup>
24+
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
25+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
8+
namespace NETCoreLogging
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
var host = new WebHostBuilder()
15+
.UseKestrel()
16+
.UseContentRoot(Directory.GetCurrentDirectory())
17+
.UseIISIntegration()
18+
.UseStartup<Startup>()
19+
.Build();
20+
21+
host.Run();
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:5382/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"NETCoreLogging": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"launchUrl": "http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using System.Text;
11+
using NLog.Extensions.Logging;
12+
13+
namespace NETCoreLogging
14+
{
15+
public class Startup
16+
{
17+
public Startup(IHostingEnvironment env)
18+
{
19+
var builder = new ConfigurationBuilder()
20+
.SetBasePath(env.ContentRootPath)
21+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
22+
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
23+
.AddEnvironmentVariables();
24+
Configuration = builder.Build();
25+
}
26+
27+
public IConfigurationRoot Configuration { get; }
28+
29+
// This method gets called by the runtime. Use this method to add services to the container.
30+
public void ConfigureServices(IServiceCollection services)
31+
{
32+
// Add framework services.
33+
services.AddMvc();
34+
}
35+
36+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
37+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
38+
{
39+
loggerFactory.AddNLog();//添加NLog
40+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
41+
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
42+
loggerFactory.AddDebug();
43+
44+
if (env.IsDevelopment())
45+
{
46+
app.UseDeveloperExceptionPage();
47+
app.UseBrowserLink();
48+
}
49+
else
50+
{
51+
app.UseExceptionHandler("/Home/Error");
52+
}
53+
54+
app.UseStaticFiles();
55+
56+
app.UseMvc(routes =>
57+
{
58+
routes.MapRoute(
59+
name: "default",
60+
template: "{controller=Home}/{action=Index}/{id?}");
61+
});
62+
}
63+
}
64+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@{
2+
ViewData["Title"] = "About";
3+
}
4+
<h2>@ViewData["Title"].</h2>
5+
<h3>@ViewData["Message"]</h3>
6+
7+
<p>Use this area to provide additional information.</p>

0 commit comments

Comments
 (0)