(精华)2020年7月20日 ASP.NET Core serilog日志框架的使用
【摘要】
首先安装如下两个包
Install-Package Serilog.AspNetCore Install-Package Serilog.Sinks.Elasticsearch
封装使用
...
首先安装如下两个包
Install-Package Serilog.AspNetCore
Install-Package Serilog.Sinks.Elasticsearch
封装使用
/// <summary>
/// 配置日志
/// </summary>
/// <param name="hostBuilder">建造者</param>
/// <returns></returns>
public static IHostBuilder UseLog(this IHostBuilder hostBuilder)
{
var rootPath = AppDomain.CurrentDomain.BaseDirectory;
var path = Path.Combine(rootPath, "logs", "log.txt");
return hostBuilder.UseSerilog((hostingContext, serilogConfig) =>
{
var envConfig = hostingContext.Configuration;
LogConfig logConfig = new LogConfig();
envConfig.GetSection("log").Bind(logConfig);
logConfig.overrides.ForEach(aOverride =>
{
serilogConfig.MinimumLevel.Override(aOverride.source, aOverride.minlevel.ToEnum<LogEventLevel>());
});
serilogConfig.MinimumLevel.Is(logConfig.minlevel.ToEnum<LogEventLevel>());
if (logConfig.console.enabled)
serilogConfig.WriteTo.Console();
if (logConfig.debug.enabled)
serilogConfig.WriteTo.Debug();
if (logConfig.file.enabled)
{
string template = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3} {SourceContext:l}] {Message:lj}{NewLine}{Exception}";
serilogConfig.WriteTo.File(
path,
outputTemplate: template,
rollingInterval: RollingInterval.Day,
shared: true,
fileSizeLimitBytes: 10 * 1024 * 1024,
rollOnFileSizeLimit: true
);
}
if (logConfig.elasticsearch.enabled)
{
var uris = logConfig.elasticsearch.nodes.Select(x => new Uri(x)).ToList();
serilogConfig.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(uris)
{
IndexFormat = logConfig.elasticsearch.indexformat,
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7
});
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseIdHelper()//分布式自增Id组件
.UseLog()//使用serilog日志框架
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace CORE.WEBERP.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogInformation("开始");
int a = 0;
int b = 2;
try
{
_logger.LogDebug("计算两者相除");
Console.WriteLine(b / a);
}
catch (Exception ex)
{
_logger.LogError(ex, "计算出现意外的错误");
}
_logger.LogInformation("结束");
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
备注:其他函数请移到类库中去查看,本人博客里都有。
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/107461937
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)