设为首页 加入收藏

TOP

Exceptionless(二) - 使用进阶(三)
2019-09-17 17:14:21 】 浏览:56
Tags:Exceptionless 使用 进阶
arams
string[] tags); void Trace(string message, params string[] tags); void Warn(string message, params string[] tags); }
using Exceptionless;
using Exceptionless.Logging;
public class ExceptionlessLogger : ILogger
{
    public void Debug(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Debug).AddTags(tags).Submit();
    }

    public void Error(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Error).AddTags(tags).Submit();
    }

    public void Fatal(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Fatal).AddTags(tags).Submit();
    }

    public void Info(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Info).AddTags(tags).Submit();
    }

    public void Off(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Off).AddTags(tags).Submit();
    }

    public void Other(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Other).AddTags(tags).Submit();
    }

    public void Trace(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Trace).AddTags(tags).Submit();
    }

    public void Warn(string message, params string[] tags) {
        ExceptionlessClient.Default.CreateLog(message, LogLevel.Warn).AddTags(tags).Submit();
    }
}

然后在Startup.csConfigureServices方法注入ExceptionlessLogger

public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton<ILogger, ExceptionlessLogger>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

这样就可以更方便地使用了

public class ValuesController : ControllerBase
{
    public ILogger _logger;
    public ValuesController(ILogger logger) {
        _logger = logger;
    }
    
    // GET api/values/{id}
    [HttpGet("{id}")]
    public ActionResult<string> Get(int id) {
        try
        {
            _logger.Info("Test msg", "tag1", "tag2");
            throw new Exception();
        }
        catch (Exception ex)
        {
            ex.ToExceptionless().AddTags("tag1", "tag2").Submit();
        }
        return $"value {id}";
    }
}

Broken Links
记录404找不到请求的日志

像我这里没有添加favicon.ico图标,使用Chrome浏览器会自动请求这个资源,因此,Exceptionless就记录了这样的日志

less_30_brokenlinks

也可以直接在Api服务中调用如下面语句添加这种类型的日志

ExceptionlessClient.Default.CreateNotFound("404 not found").SetType("404").SetSource($"api/values/{id}");

Feature Usages
类似的也可以添加Feature Usages日志

ExceptionlessClient.Default.CreateFeatureUsage("Feature 1").SetSource($"api/values/{id}").SetType("FeatureType").Submit();

事件

上面所说的所有日志类型,最终都会通过事件进行记录,Exceptionless也支持我们直接记录一个事件

例子如下:

var dataDic = new Exceptionless.Models.DataDictionary();
dataDic.Add("key", "value");
ExceptionlessClient.Default.SubmitEvent(new Exceptionless.Models.Event
{
    Count = 1,
    Date = DateTime.Now,
    Data = dataDic,
    Geo = "geo",
    Message = "message",
    ReferenceId = "referencelId",
    Source = "source",
    Tags = new Exceptionless.Models.TagSet() { "tags" },
    Type = "type"
});

Exceptionless同时也支持我们捕获事件提交过程和事件提交后的事件,这样我们就可以在过程中做一些操作,

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Asp.NetCoreWebApi入门 - 从零开.. 下一篇.NET架构师知识普及

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目