Furion之生产日志,异常日志记录

前言:在日常工作做,我们不仅仅只需要让程序跑起来就行(还得人会跑才行),开个玩笑,在正常的生产环境下,往往会出现一些突发情况,导致程序异常,数据异常,导致对用户影响极大,其次我们在找如果有相关异常信息,和异常数据,那我们分析和解决这个bug将会事半功倍,所以,接下来我演示下,最近正在研究的Furion 的日志记录,当然我只是示例其中的一个,一部分,有兴趣的童鞋,可以前往Furion官方文档进行学习!我将会在上一张的基础上进行今天的专题操作

1.:首先微软已经为我们内置了 日志组件,正常情况下,无需我们引用第三方包进行日志记录,我们可以直接构造注入使用

​
  /// <summary>
    /// 某模块接口
    /// </summary>
    [ApiDescriptionSettings("某模块接口")]
    public class Furion2AppService : IDynamicApiController
    {
        private readonly ILogger<Furion2AppService> _logger;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="logger"></param>
        public Furion2AppService(ILogger<Furion2AppService> logger)
        {
           
            this._logger = logger;
          
        }

​

2.我们可以通知这写方法来进行日志记录

_logger.Information("Information");
_logger.Warning("Warning");
_logger.Error("Error");
_logger.Debug("Debug");
_logger.Trace("Trace");
_logger.Critical("Critical");

3.正常情况下,这时候日志只会记录到控制面板上,那我们怎么才能让保存到文件呢

找到appsettings.json ,查看有没有这段json ,或者一样不

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.EntityFrameworkCore": "Information"
    }
  }

如果在生产中使用,应配置 appsettings.Development.json

4.那么日志组件如何自定义路径文件格式呢,这里我们修改 Program.cs

using System.Text.Encodings.Web;

var builder = WebApplication.CreateBuilder(args).Inject();

builder.Services.AddJsonOptions(options =>
                    {
                         //定义json编码格式,处理中文乱码
                        options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                    });
///生产日志
builder.Services.AddFileLogging("logs/application/application-{0:yyyy}-{0:MM}-{0:dd}.log", options =>
{
    options.FileNameRule = fileName =>
    {
        return string.Format(fileName, DateTime.UtcNow);
    };
    options.WriteFilter = (logMsg) =>
    {
        return logMsg.LogLevel == LogLevel.Information;
    };
});
//异常日志
builder.Services.AddFileLogging("logs/error/error-{0:yyyy}-{0:MM}-{0:dd}.log", options =>
{
    options.FileNameRule = fileName =>
    {
        return string.Format(fileName, DateTime.UtcNow);
    };
    options.WriteFilter = (logMsg) =>
    {
        return logMsg.LogLevel == LogLevel.Error;
    };
});
builder.Services.AddControllers().AddInject();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseInject("Myapi");
app.MapControllers();
app.Run();

这里我只定义了两种类型的日志,如需要更多请前往Furion官方文档

5.我们也可以直接使用Furion提供的日志模板

    var template = TP.Wrapper("填充日志记录", "有数据进入", "##时间##" + DateTime.Now);
            _logger.LogInformation(template);

示例代码:

using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.Templates;
using Microsoft.AspNetCore.Mvc;
//日志
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace F2.Application
{
    [ApiDescriptionSettings("友好异常处理")]
    public class FurionAppService : IDynamicApiController
    {
       
        public string Getwhat(int num)
        {
            string[] str= new string[] { "222", "333" };
            if (str.Count() > num) 
                return str[num];
            else
            //string errorMessage = $"{num} 不能大于等于{str.Count()}";
            throw Oops.Oh(-2003);
           
        }
        public string Getwhata(int num)
        {
            string[] str = new string[] { "222", "333" };
          
                return str[7];
          

        }
    }

    /// <summary>
    /// 某模块接口
    /// </summary>
    [ApiDescriptionSettings("某模块接口")]
    public class Furion2AppService : IDynamicApiController
    {
        private readonly ILogger<Furion2AppService> _logger;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="logger"></param>
        public Furion2AppService(ILogger<Furion2AppService> logger)
        {
           
            this._logger = logger;
          
        }
        /// <summary>
        /// 接口1
        /// </summary>
        /// <returns></returns>
        public string Getword()
        {
            var template = TP.Wrapper("填充日志记录", "有数据进入", "##时间##" + DateTime.Now);
            _logger.LogInformation(template);
            return "str";
        }
        /// <summary>
        /// 接口2
        /// </summary>
        /// <returns></returns>
        public string Getword2()
        {

            return "str";
        }
    }
}

日志生成示例

下章:Furion之 Sqlserver数据库+Microsoft.EntityFrameworkCore操作

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值