Exceptionless (two) - Advanced Use
Author: markjiang7m2
original address: https://www.cnblogs.com/markjiang7m2/p/11100563.html
official website address: http://letyouknow.net
In the previous article Exceptionless - .Net Core open source logging framework in Exceptionless spoke of how to localize the deployment, but I tell you is that, and is limited to the use of the stage. Well, today I will continue to explore how to reuse good.
Services running in the background
The last time I direct a script that Start-ElasticSearch.ps1
starts ElasticSearch and Kibana service, but we can also see that the service is up and running, as well as two command window, if a window is closed accidentally, the service will shut down, and Once the server is restarted, these two services will not start automatically.
I'm here to temporarily put aside Kibana throw, take a look at ElasticSearch services. ElasticSearch there is a direct script support ElasticSearch installed as a Windows service that runs in the background.
This is the elasticsearch-service.bat
script to support what parameters:
- install will install as a service Elasticsearch
- remove remove installed Elasticsearch service (and stop the service when started)
- start start Elasticsearch service (if installed)
- stop stop Elasticsearch service (if started)
- manager to start a GUI to manage the installed service
Installation
command line, enter the elasticsearch-service.bat
directory resides, and then execute the following script
elasticsearch-service.bat install
start up
This time we can directly access the browser 9200
interface to see whether the normal service
Continue to use the last deployed Exceptionless
(how to use IIS to deploy Exceptionless Web services, see Exceptionless - .Net Core open source logging framework )
Because I was directly before returning ElasticSearch nodes, and there is no clear the data, so you can log in with a registered account before re-direct
Also successful, ElasticSearch service is already running in the background.
Auto Start
GUI can be provided by ElasticSearch service set to start automatically
elasticsearch-service.bat manager
The Startup Type
choice is Automatic
, and then click OK to save
Thus, even if the server is restarted, and our service will automatically start ElasticSearch
In fact, Windows services have played friends must know, some of the operations above comes in Windows Service Manager can be completed
Press the command "WIN + R" to open the Run window services. Enter services.msc in the Run window service
The list can also be found ElasticSearch service, double-click to open the Properties window, with the GUI operation is just the same
Look Web.config
Last time I just changed the port settings Exceptionless, in fact, there is also contains a lot of configuration information
<add name="RedisConnectionString" connectionString="localhost:6379,abortConnect=false" />
<add name="ElasticSearchConnectionString" connectionString="http://localhost:9200" />
The official is recommended that you install and configure Redis, so that you can run multiple instances and restart without losing state, it is strongly recommended that Redis 3.0+ version to run on Linux, RedisConnectionString
is the connection string of Redis
ElasticSearchConnectionString
Is necessary, point ElasticSearch service, if there are multiple nodes, use ,
separated
<!-- Exceptionless Web 基础Url -->
<add key="BaseURL" value="http://localhost:50001/#" />
<!-- 是否启用ssl -->
<add key="EnableSSL" value="false" />
<!--
Dev: Use this mode when debugging. (Outbound emails will not be sent)
QA: Use this mode when deployed to staging. (Outbound emails restricted)
Production: Use this mode when deployed to production.
-->
<add key="WebsiteMode" value="Production" />
<!-- Controls whether users can signup. -->
<add key="EnableAccountCreation" value="true" />
<!-- Controls whether daily summary emails are sent -->
<add key="EnableDailySummary" value="true" />
网站模式WebsiteMode主要是限制邮件发送,默认值是Dev,不发送邮件,所以我这里设置为Production
邮件发送配置,记得跟上面的WebsiteMode一起配置
<add key="SmtpHost" value="smtp.qq.com" />
<add key="SmtpPort" value="25" />
<add key="SmtpEncryption" value="SSL" />
<add key="SmtpUser" value="[email protected]" />
<add key="SmtpPassword" value="xxx" />
我在案例中使用的是自己的qq邮箱。我在qq邮箱中已经开启了SMTP服务,并且也通过一个控制台应用程序测试可以发送邮件。
但是在Exceptionless这里一样的设置就是不行,在Web中点击发送邮件,log记录错误如下:
ERROR MailMessageJob Job run "MailMessageJob" failed: 由于意外的数据包格式,握手失败。 System.IO.IOException: 由于意外的数据包格式,握手失败。
清除Url
我们现在使用的Url都会带有#!
,例如
http://localhost:50001/#!/type/error/dashboard
可以按照下面步骤清除字符#!
- 首先保证你的IIS是否已经安装了重写模块,可通过双击IIS中的
模块
查看是否包含了RewriteModule
- 更新
Web.config
文件- 释放出在
system.webServer
中的rewrite
节点 - 删除
BaseURL
值中的/#
- 注释了在
system.webServer\modules
中的<remove name="RewriteModule" />
标签
- 释放出在
- 修改
app.config.77fc9ddd679d37dc.js
文件中USE_HTML5_MODE
的值为true
进程外运行作业
默认情况下,所有作业都在当前的Web进程中运行。如果发现事件处理开始变慢的时候,可以启动并扩展多个作业实例。通过在进程外运行作业,可以确保所有作业是否正常运行。
- 首先是要配置安装Redis,这样可以保证Exceptionless与作业之间能够进行通信
- 更新
Web.config
中的RunJobsInProcess
值为false
- 更新作业的配置,有两种方法可选:
- 使用环境变量进行配置Exceptionless。新增环境变量Exceptionless_{SETTING NAME} (例如: Exceptionless_BaseURL, Exceptionless_ElasticSearchConnectionString)。这是官方推荐的方法,因为它更简单,而且当部署到azure时非常好用
- 打开App_Data\jobs文件夹,然后按照在根目录中
Web.config
的配置,再重新配置每个作业的xxx.exe.config
。
- 在每个作业文件夹中都有一个
run.bat
文件,双击它就会运行这个作业。当然,也可以将这些作业全部设置为Windows服务在后台运行
更多设置
除了上面提到的设置,Exceptionless还支持很多自定义配置,下面是全部的设置列表,大家可根据自己的需要进行定制
列表按照这个格式进行排列:设置项 (数据类型,默认值)
EnableSSL (bool)
BaseURL (string)
InternalProjectId (string, "54b56e480ef9605a88a13153")
WebsiteMode (WebsiteMode, "Dev")
AppScope (string, String.Empty)
TestEmailAddress (string)
AllowedOutboundAddresses (List<string>, "exceptionless.io")
RunJobsInProcess (bool, true)
BotThrottleLimit (int, 25)
ApiThrottleLimit (int, Int32.MaxValue)
EventSubmissionDisabled (bool)
MaximumEventPostSize (long, 1000000)
MaximumRetentionDays (int, 180)
EnableDailySummary (bool)
MetricsServerName (string, "127.0.0.1")
MetricsServerPort (int, 8125)
EnableMetricsReporting (bool)
RedisConnectionString (string)
EnableRedis (bool)
DisableSnapshotJobs (bool)
DisableIndexConfiguration (bool)
ElasticSearchConnectionString (string)
ElasticSearchNumberOfShards (int, 1)
ElasticSearchNumberOfReplicas (int)
EnableElasticsearchTracing (bool)
LdapConnectionString (string)
EnableActiveDirectoryAuth (bool)
EnableSignalR (bool, true)
Version (string)
EnableIntercom (bool)
IntercomAppSecret (string)
EnableAccountCreation (bool, true)
MicrosoftAppId (string)
MicrosoftAppSecret (string)
FacebookAppId (string)
FacebookAppSecret (string)
GitHubAppId (string)
GitHubAppSecret (string)
GoogleAppId (string)
GoogleAppSecret (string)
GoogleGeocodingApiKey (string)
EnableBilling (bool)
StripeApiKey (string)
StorageFolder (string)
AzureStorageConnectionString (string)
EnableAzureStorage (bool)
BulkBatchSize (int, 1000)
SmtpHost (string)
SmtpPort (int, 587)
SmtpEnableSsl (bool, true)
SmtpUser (string)
SmtpPassword (string)
更多日志类型
Exceptionless除了支持记录Exception,也可以记录LogMessage、Broken Links 、Feature Usages
LogMessage
LogMessage支持多种级别的日志信息
- Other
- Trace
- Debug
- Info
- Warn
- Error
- Fatal
- Off
用法也很简单,直接在你想要记录日志的地方直接加一句
ExceptionlessClient.Default.CreateLog("日志信息", LogLevel.Debug).AddTags("tag1", "tag2").Submit();
所以我们在应用的过程中,可以添加一个统一的接口
public interface ILogger
{
void Debug(string message, params string[] tags);
void Error(string message, params string[] tags);
void Fatal(string message, params string[] tags);
void Info(string message, params string[] tags);
void Off(string message, params string[] tags);
void Other(string message, params 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;
}
// 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就记录了这样的日志
也可以直接在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同时也支持我们捕获事件提交过程和事件提交后的事件,这样我们就可以在过程中做一些操作,例如可以忽略404
的请求,或者针对某些特殊日志返回某些信息
为了代码的整洁,可以将Exceptionless的配置单独放到一个cs文件中
添加一个ExceptionlessBuilderExtensions
类
public static class ExceptionlessBuilderExtensions
{
public static IApplicationBuilder UseExceptionless(this IApplicationBuilder app, IConfiguration configuration)
{
ExceptionlessClient.Default.Configuration.ApiKey = configuration["Exceptionless:ApiKey"];
ExceptionlessClient.Default.Configuration.ServerUrl = configuration["Exceptionless:ServerUrl"];
ExceptionlessClient.Default.SubmittingEvent += OnSubmittingEvent;
app.UseExceptionless();
return app;
}
private static void OnSubmittingEvent(object sender, EventSubmittingEventArgs e)
{
if (e.Event.IsNotFound())
{
e.Cancel = true;//取消事件提交
return;
}
// 修改日志信息
if (e.Event.Source == "sourceA")
{
e.Event.AddTags("systemLog");
}
//TODO:
}
private static void OnSubmittedEvent(object sender, EventSubmittedEventArgs e)
{
// 做点什么东西
if (e.Event.Source == "sourceA")
{
//TODO:
}
}
}
然后修改Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
……
app.UseExceptionless(Configuration);
……
}
Exceptionless 日志查询
Exceptionless Web站点已经帮我们做好项目、时间、日志类型的分类,大家可以很直观地进行操作查询。
我这里要关注的是Filter
查询
前面记录日志的时候,有添加了tag
、Type
等信息,这时候就可以使用Filter进行查询了。
语法:
[FilterType]:[value1] {or} {[value2]}
or是用于查询多个该类型值的日志时使用
例如:tag:tag1
列几个可能比较常用的
- source:"my log source" or "my log source"
- type:error
- level:Error
- ip:127.0.0.1
如果是要同时输入多种类型的条件:
[FilterType]:[value] {OR|AND} {[FilterType]:[value]}
例如:tag:tag1 OR ip:127.0.0.1
更多的语法可以看官网说明
https://github.com/exceptionless/Exceptionless/wiki/Filtering-Searching
总结
In this article, I basically was introduced along Exceptionless Self Hosting to do it again, but there are some things because there is no practical environment, so it does not do it, then I this is only a Demo, temporarily did not do related stress test, so I do not know that the goods have some really what a lot of performance in a production environment when used together, will not step on what pit. Welcome to share with me in the comments section. Today to talk to you about here, I hope you can continue to concern us.
References
cited herein in reference to the preparation process or part of the following article, if infringement, please contact modified or deleted.
https://www.cnblogs.com/edisonchou/p/exceptionless_deployment_on_production_env_introduction.html
https://www.cnblogs.com/ants/p/8580890.html