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.cs
的ConfigureServices
方法注入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;
}
[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就记录了这样的日志
也可以直接在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同时也支持我们捕获事件提交过程和事件提交后的事件,这样我们就可以在过程中做一些操作,