添加 Redis

master
bicijinlian 5 years ago
parent 80389059ee
commit 8c3f4b78e5

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace NLogStudy.CoreWeb2.UseRedis.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger logger;
public ValuesController(ILogger<ValuesController> logger)
{
this.logger = logger;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
logger.LogError("====我是测试信息========================");
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="NLog" Version="4.6.7" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
<PackageReference Include="NLog.Targets.Redis" Version="2.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
</ItemGroup>
<ItemGroup>
<None Update="NLog.xsd">
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NLog.Web;
namespace NLogStudy.CoreWeb2.UseRedis
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostBuild, logBuild) =>
{
logBuild.ClearProviders();
logBuild.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug);
})
.UseNLog();
}
}

@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61384",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"NLogStudy.CoreWeb2.UseRedis": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace NLogStudy.CoreWeb2.UseRedis
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
keepVariablesOnReload="false"
throwExceptions="true"
throwConfigExceptions="true"
internalLogLevel="Trace"
internalLogFile="${basedir}\logs\internal-nlog.txt"
>
<!--引入Nlog扩展包-->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<!--<add assembly="NLog.Redis"/>-->
<add assembly="NLog.Targets.Redis" />
</extensions>
<!--自定义变量-->
<variable name="myvariable" value="test" />
<!--设置目标-->
<targets>
<target xsi:type="Redis"
name="redis"
host="127.0.0.1"
port="6379"
db="0"
key="logKey"
dataType="list"
layout="${date:format=yyyyMMddHHmmss} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="redis" />
</rules>
</nlog>

@ -21,6 +21,7 @@ namespace NLogStudy.WebCore2.Study.Controllers
public ActionResult<IEnumerable<string>> Get() public ActionResult<IEnumerable<string>> Get()
{ {
logger.LogError("我是测试错误"); logger.LogError("我是测试错误");
logger.LogDebug("我是Debug");
return new string[] { "value1", "value2" }; return new string[] { "value1", "value2" };
} }

@ -25,8 +25,10 @@ namespace NLogStudy.WebCore2.Study
.ConfigureLogging((hostBuild,logBuild)=> { .ConfigureLogging((hostBuild,logBuild)=> {
//清除自带的Providers //清除自带的Providers
logBuild.ClearProviders(); logBuild.ClearProviders();
//默认最小级别 //保留Debug窗口信息
logBuild.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug); //logBuild.AddDebug();
//默认最小级别:会被Nlog.config配置项重写
logBuild.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
}) })
.UseNLog(); .UseNLog();
} }

@ -1,7 +1,7 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Debug", "Default": "Warning",
"System": "Information", "System": "Information",
"Microsoft": "Information" "Microsoft": "Information"
} }

@ -3,9 +3,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true" autoReload="true"
keepVariablesOnReload="false"
throwExceptions="true" throwExceptions="true"
throwConfigExceptions="true" throwConfigExceptions="true"
internalLogLevel="Info" internalLogLevel="Trace"
internalLogFile="${basedir}\logs\internal-nlog.txt" internalLogFile="${basedir}\logs\internal-nlog.txt"
> >
<!--引入Nlog扩展包--> <!--引入Nlog扩展包-->
@ -19,7 +20,18 @@
<!--设置目标--> <!--设置目标-->
<targets> <targets>
<!--默认彩色控制台--> <!--默认彩色控制台-->
<target xsi:type="ColoredConsole" name="DefaultColoredConsole"/> <target xsi:type="ColoredConsole"
name="ColoredConsoleLog"
encoding="utf-8"
header="-------------------------------------------"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
footer="############################################"
/>
<!--默认彩色控制台-->
<target xsi:type="Console" name="ConsoleLog" />
<target name="debugLog" xsi:type="Debugger" layout="${logger}::${message}"/>
<!--写入日志文件--> <!--写入日志文件-->
<target xsi:type="File" <target xsi:type="File"
@ -29,13 +41,18 @@
<!-- 写入文件使用asp.net core --> <!-- 写入文件使用asp.net core -->
<target xsi:type="File" <target xsi:type="File"
name="ownFile-web" name="webFilelog"
fileName="${basedir}\logs\nlog-own-${shortdate}.log" fileName="${basedir}\logs\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets> </targets>
<!--设置规则--> <!--设置规则-->
<rules> <rules>
<logger ruleName="*" minlevel="Trace" writeTo="DefaultColoredConsole" /> <logger ruleName="*" minlevel="Warn" writeTo="ColoredConsoleLog" />
<logger ruleName="*" minlevel="Trace" writeTo="debugLog" />
<!--<logger ruleName="System*" minlevel="Trace" writeTo="ColoredConsoleLog" />-->
<!--<logger ruleName="*" minlevel="Trace" writeTo="ConsoleLog" />
<logger ruleName="*" minlevel="Trace" writeTo="allfile" />
<logger ruleName="*" minlevel="Trace" writeTo="webFilelog" />-->
</rules> </rules>
</nlog> </nlog>

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.29424.173 VisualStudioVersion = 16.0.29424.173
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLogStudy.CoreWeb2.Default", "NLogStudy.CoreWeb2.Default\NLogStudy.CoreWeb2.Default.csproj", "{2A278A28-28C4-4A90-9BFF-BE85292DD40F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLogStudy.CoreWeb2.Default", "NLogStudy.CoreWeb2.Default\NLogStudy.CoreWeb2.Default.csproj", "{2A278A28-28C4-4A90-9BFF-BE85292DD40F}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetCore2", "NetCore2", "{A4009762-9525-445C-AE31-7D240F3E2BA4}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetCore2", "NetCore2", "{A4009762-9525-445C-AE31-7D240F3E2BA4}"
EndProject EndProject
@ -14,13 +14,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{
Study.md = Study.md Study.md = Study.md
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLogStudy.WebCore2.Database", "NLogStudy.WebCore2.Database\NLogStudy.WebCore2.Database.csproj", "{E6F61C33-1123-4CE0-A67F-CB22312E4B6E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLogStudy.WebCore2.Database", "NLogStudy.WebCore2.Database\NLogStudy.WebCore2.Database.csproj", "{E6F61C33-1123-4CE0-A67F-CB22312E4B6E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLogStudy.WebCore2.Study", "NLogStudy.WebCore2.Study\NLogStudy.WebCore2.Study.csproj", "{C1435088-3E35-47AB-9672-3EA9813771ED}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLogStudy.WebCore2.Study", "NLogStudy.WebCore2.Study\NLogStudy.WebCore2.Study.csproj", "{C1435088-3E35-47AB-9672-3EA9813771ED}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetFramework", "NetFramework", "{A4C5C2CC-8F8A-49C9-AAF6-AADA380791F0}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetFramework", "NetFramework", "{A4C5C2CC-8F8A-49C9-AAF6-AADA380791F0}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLogStudy.CoreApp2.ConsoleApp", "NLogStudy.CoreApp2.ConsoleApp\NLogStudy.CoreApp2.ConsoleApp.csproj", "{36486C33-686F-4289-A393-9638221F597D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLogStudy.CoreApp2.ConsoleApp", "NLogStudy.CoreApp2.ConsoleApp\NLogStudy.CoreApp2.ConsoleApp.csproj", "{36486C33-686F-4289-A393-9638221F597D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLogStudy.CoreWeb2.UseRedis", "NLogStudy.CoreWeb2.UseRedis\NLogStudy.CoreWeb2.UseRedis.csproj", "{A09AE790-7201-42D2-A489-92F3C2C937EB}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -44,6 +46,10 @@ Global
{36486C33-686F-4289-A393-9638221F597D}.Debug|Any CPU.Build.0 = Debug|Any CPU {36486C33-686F-4289-A393-9638221F597D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36486C33-686F-4289-A393-9638221F597D}.Release|Any CPU.ActiveCfg = Release|Any CPU {36486C33-686F-4289-A393-9638221F597D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36486C33-686F-4289-A393-9638221F597D}.Release|Any CPU.Build.0 = Release|Any CPU {36486C33-686F-4289-A393-9638221F597D}.Release|Any CPU.Build.0 = Release|Any CPU
{A09AE790-7201-42D2-A489-92F3C2C937EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A09AE790-7201-42D2-A489-92F3C2C937EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A09AE790-7201-42D2-A489-92F3C2C937EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A09AE790-7201-42D2-A489-92F3C2C937EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -53,6 +59,7 @@ Global
{E6F61C33-1123-4CE0-A67F-CB22312E4B6E} = {A4009762-9525-445C-AE31-7D240F3E2BA4} {E6F61C33-1123-4CE0-A67F-CB22312E4B6E} = {A4009762-9525-445C-AE31-7D240F3E2BA4}
{C1435088-3E35-47AB-9672-3EA9813771ED} = {A4009762-9525-445C-AE31-7D240F3E2BA4} {C1435088-3E35-47AB-9672-3EA9813771ED} = {A4009762-9525-445C-AE31-7D240F3E2BA4}
{36486C33-686F-4289-A393-9638221F597D} = {A4009762-9525-445C-AE31-7D240F3E2BA4} {36486C33-686F-4289-A393-9638221F597D} = {A4009762-9525-445C-AE31-7D240F3E2BA4}
{A09AE790-7201-42D2-A489-92F3C2C937EB} = {A4009762-9525-445C-AE31-7D240F3E2BA4}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EB3CF482-270A-4536-9A9B-EA1145B7E859} SolutionGuid = {EB3CF482-270A-4536-9A9B-EA1145B7E859}

Loading…
Cancel
Save