基于.net core webapi和mongodb的日志系统
开发环境vs2017,.NET Core2.1, 数据写入到mongodb。思路就是1.提供接口写入日志,2.基于接口封装类库。3.引入类库使用
为什么要写它
很多开源项目像nlog、log4net、elk、exceptionless等都挺好的。就是除了引入所需类库,还要在项目中添加配置,不喜欢。elk在分布式海量数据收集和检索方面可能更能发挥它的优势,单纯记日志也可以,exceptionless就是基于elk的。就想着写一个简单易用的、可以发邮件报警的,直接引入类库就能用的一个记日志工具,所有的配置信息和入库都交给web api。这是当时问的问题,upload/201809201139180740.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" />
{ "ConnectionStrings": { "ConnectionString": "mongodb://yourmongoserver", "Database": "logdb", "LogCollection": "logdata" }, "AllowedHosts": "*", "AppSettings": { "SendMailInfo": { "SMTPServerName": "smtp.qiye.163.com", "SendEmailAdress": "发送人邮箱", "SendEmailPwd": "", "SiteName": "邮件主题", "SendEmailPort": "123" } }} 接下来Here is how we modify Startup.cs to inject Settings in the Options accessor model:
IOptions<AppSettings> IOptions<DBSettings>
services.AddTransient<IRepository<LogEventData>, LogRepository>();//数据访问