Opserver 初探二《exceptions配置》
上一节主要介绍Opserver的搭建以及redis、sqlserver监控的配置,本节主要介绍异常日志的记录和监控。要实现异常日志的监控我们需要在项目中引入StackExchange.Exceptional组件,用于日志的采集和存储操作。
首先我们简单的来认识一下Opserver项目config文件夹下的ExceptionsSettings.json.example文件,
{
"warningRecentCount": "100", //警告提醒最近条目数,当超出该值在头部高亮显示警告
"criticalRecentCount": "200", //严重警告提醒最近条目数,当超出该值在头部高亮显示严重警告
"viewGroups": "StatusExceptionsRO", //安全模式“ad"下的分组查看权限设置
// You can have a simple applications list here, or a grouped structure when dealing with many apps.
//"applications": [ //产生异常日志的程序
// "Core",
// "Chat",
// "Stack Auth",
// "StackExchange.com",
// "API",
// "API v2",
// "Area 51",
// "Status",
// "Push Server",
// "Sockets",
// "Careers",
// "BackOffice",
// "Control Panel"
//],
//以分组在形式归类异常日志,未在groups定义的在others中显示
"groups": [
{
"name": "Core Q&A",
"applications": [
"Core",
"Chat",
"Stack Auth",
"StackExchange.com",
"API v2",
"Sockets",
"Area 51",
"Open ID",
"Stack Server",
"StackSnippets",
"Status"
]
},
{
"name": "Careers",
"applications": [
"Careers",
"BackOffice",
"BackOffice",
"Control Panel",
"StackShop",
"CareersAuth"
]
},
{
"name": "Mobile",
"applications": [
"Push Server"
]
},
{
"name": "Ads & Data",
"applications": [
"Prov Read API"
]
}
],
"stores": [ //异常日志存储位置
{
"name": "New York",
"queryTimeoutMs": 2000,
"pollIntervalSeconds": 10,
"connectionString": "Server=ny-sql03.ds.stackexchange.com;Database=NY.Exceptions;Integrated Security=SSPI;"
}
]
}
1、StackExchange.Exceptional引入及记录异常日志
异常日志可以有web,console,service等程序产生
web项目,需要3步配置:
- 在web项目中nuget安装StackExchange.Exceptional组件(依赖dapper)
- 在web.config中增加section “Exceptional”
<section name="Exceptional" type="StackExchange.Exceptional.Settings" />
<Exceptional applicationName="Samples.MVC4">
<ErrorStore type="Memory" />
<!--连接opserver数据库时开启-->
<!--<ErrorStore type="SQL" connectionString="Server=.;Database=Exceptions;Uid=Exceptions;Pwd=myPassword!" />-->
</Exceptional>
applicatinName 产生异常日志的程序名称用于Opserve显示归类
ErrorStore 错误存储有4种实现方式,Memory,JSON,SQL,MySQL,参考官方说明
<!-- Which ErrorStore to use, if no element is declared here a Memory store with defaults will be used -->
<!--<ErrorStore type="Memory" />-->
<!-- Other store types, common attributes:
- rollupSeconds: optional (default 600 seconds), determines how long the window is to roll up exceptions with the same stack trace - 0 to not roll up
- backupQueueSize: optional (default 1000), determines how many errors to cache (excluding rollups) in memory when logging fails...every 2 seconds we'll retry logging and flush these out to the final store -->
<!-- JSON: size defaults to 200, this is how many files are kept before the oldest error is removed -->
<!--<ErrorStore type="JSON" path="~/Errors" size="200" />-->
<!-- SQL: only a connection string or connection string name is needed, many applications can log to the same place as long as they have unique names (otherwise we can't tell them apart). -->
<!--<ErrorStore type="SQL" connectionString="Server=.;Database=Exceptions;Uid=Exceptions;Pwd=myPassword!" />-->
<!--<ErrorStore type="SQL" connectionStringName="MyConnectionString" />-->
<!-- You can also use a MySQL Database with the MySQL ErorrStore -->
<!--<ErrorStore type="MySQL" connectionString="Server=.;Database=Exceptions;Username=Exceptions;Pwd=myPassword!" />-->
<!--<ErrorStore type="MySQL" connectionStringName="MyConnectionString" />-->
- 在web.config中增加handler和module,module配置后,异常日志自动记录
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<!-- OPTIONAL: if not using a router, or you don't want to access the errors directly via this application,
this is not necessary. A common scenario for this is logging all errors to a common SQL store and viewing
them through another application or a dashboard elsewhere.
Note: If the error list isn't exposed in the application, any errors in logging exceptions will be queued up,
but not visible (since that's exposed on the error list view).
-->
<add name="Exceptional" path="exceptions.axd" verb="POST,GET,HEAD" type="StackExchange.Exceptional.HandlerFactory, StackExchange.Exceptional" preCondition="integratedMode" />
</handlers>
<!-- Here the error log needs to be registered to catch all unhandled exceptions,
these are exceptions that make it all the way to being a 500 page the user sees. -->
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="StackExchange.Exceptional.ExceptionalModule, StackExchange.Exceptional" />
</modules>
</system.webServer>
console,service前两步同web项目,需要nuget安装StackExchange.Exceptional,在app.config配置section“Exceptional”,然后在main主函数中添加未处理异常记录
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
/*
...
*/
} private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
StackExchange.Exceptional.ErrorStore.LogExceptionWithoutContext(ex);
}
}
如果需要自定义添加异常,可以在try...catch中记录,如:
try
{
throw new Exception("Just a try/catch test");
}
catch (Exception ex)
{
// logged, but caught so we don't crash
ErrorStore.LogExceptionWithoutContext(ex);
}
3、异常数据存储SQL,创建表语句
USE [OpServerTest]
GO /****** Object: Table [dbo].[Exceptions] Script Date: 2016/11/16 16:28:56 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO SET ANSI_PADDING ON
GO CREATE TABLE [dbo].[Exceptions](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[GUID] [uniqueidentifier] NOT NULL,
[ApplicationName] [nvarchar](50) NOT NULL,
[MachineName] [nvarchar](50) NOT NULL,
[CreationDate] [datetime] NOT NULL,
[Type] [nvarchar](100) NOT NULL,
[IsProtected] [bit] NOT NULL,
[Host] [nvarchar](100) NULL,
[Url] [nvarchar](500) NULL,
[HTTPMethod] [nvarchar](10) NULL,
[IPAddress] [varchar](40) NULL,
[Source] [nvarchar](100) NULL,
[Message] [nvarchar](1000) NULL,
[Detail] [nvarchar](max) NULL,
[StatusCode] [int] NULL,
[SQL] [nvarchar](max) NULL,
[DeletionDate] [datetime] NULL,
[FullJson] [nvarchar](max) NULL,
[ErrorHash] [int] NULL,
[DuplicateCount] [int] NOT NULL,
CONSTRAINT [PK_Exceptions] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF
GO ALTER TABLE [dbo].[Exceptions] ADD DEFAULT ((0)) FOR [IsProtected]
GO ALTER TABLE [dbo].[Exceptions] ADD DEFAULT ((1)) FOR [DuplicateCount]
GO
4、在Opserver项目中,配置 ExceptionsSettings.json文件,修改groups和stores,如:
"stores": [ //异常日志存储位置
{
"name": "ExceptionDB",
"queryTimeoutMs": 2000,
"pollIntervalSeconds": 10,
"connectionString": "Data Source=192.168.1.120;User ID=sa;Password=*******;Initial Catalog=OpserverTest;"
}
注:目前Opserver版本支持store在sql server服务器上,不支持在文件,内存,和Mysql中,如果需要mysql支持,需要修改Opserver.core代码。
在配置多个store的情况,默认第一个有效
官方示例dome和源码下载:https://github.com/NickCraver/StackExchange.Exceptional
Opserver 初探二《exceptions配置》的更多相关文章
- Opserver 初探一《Opserver的搭建》
Opserver 是Stack Overflow的开源监控产品.stackoverflow网站是基于asp.net开发的,具体采用的软硬件配置可以查看<StackOverflow 这么大,究竟用 ...
- ThinkPHP 3.2.3(二)配置
一.配置格式 1.PHP数组定义 默认所有配置文件的定义格式均采用返回PHP数组的方式,配置参数不区分大小写. 如果使用二维数组来配置更多的信息,则二级参数配置区分大小写.格式为: //项目配置文件r ...
- Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例)
这是一个Maven提高篇的系列,包含有以下文章: Maven提高篇系列之(一)——多模块 vs 继承 Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例) ...
- AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(二):配置WinClient分布式运行环境
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- ANDROID Porting系列二、配置一个新产品
ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1. 目录//vendor/创建一个公 ...
- Ubuntu配置OpenStack 二:配置时间同步NTP和安装数据库Maridb以及问题总结
继上一节Ubuntu配置OpenStack 一:配置主机环境,下面继续为安装时间同步,以及配置openstack的安装包源和安装数据库Maridb.(全文截图都是由自己徒手搭建完成并且截图) 一.安装 ...
- CentOS 7 学习(二) 配置Nginx反向代理
CentOS 7 学习(二) 配置Nginx反向代理 Nginx可以通过php-fpm来运行PHP程序,也可以转向apache,让apache调用php程序来运行. 不过对于Nginx来说,其反向代理 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
随机推荐
- hdu-1061(快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 思路:快速幂 #include<iostream> #include<cstd ...
- 第六章 副词(Les adverbes )
副词属于不变词类,无性.数变化(tout除外),它的功能是修饰动词.形容词.副词或句子. ➡副词的构成 ⇨单一副词 bien tard hier mal vite tôt très souvent ...
- ssh到远程执行命令并返回
假设有三台机器 host1,host2,host3,host1能免密登录其他两台.要在第一台的机器里面写一个脚本分别删除每台机器的/root/test.txt rm -rf /root/test.tx ...
- 使用系统的CoreLocation定位
//// ViewController.m// LBS//// Created by tonnyhuang on 15/8/28.// Copyright (c) 2015年 tonnyhua ...
- hdu1158 Employment Planning 2016-09-11 15:14 33人阅读 评论(0) 收藏
Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Python学习-35.Python中的List Comprehensions(列表解释|列表生成式)
在某些情况下,我们需要对列表进行某些操作,例如对列表中的每一个元素都乘以2,这样一般来说就是遍历每个元素在乘以2.那么写下来就得两行了.而且这会修改原来的列表,如果要求不能修改原来的列表,又得多一行了 ...
- 理解go语言 协程之间的通讯
go已经越来越被重视了,特别适合大型互联网公司基础服务的编写,高效,高并发,可以同时允许多个明星出轨,多个明星结婚 都不在话下,下面介绍下GO协程通讯的过程 直接上代码 package main im ...
- windows服务器让WEB通过防火墙的问题
服务器环境:windows server 2012 X64WEB服务器:IIS开放8080,PHPSduty开放80 如果关闭防火墙的情况下,不论是IIS还是安装的其他的WEB服务器,都可以正常访问. ...
- 用.net开发音频编辑软件
智能语音机器人软件免不了需要对语音流进行处理,主要包括语音的播放.录制.读取.保存.图形化展示和剪辑等功能.这方面专业的软件有Adobe Audition,为了方便用户使用,我用.net实现了上述功能 ...
- Caliburn.Micro(MVVM框架)
一.首启窗体设置 1. 创建一个新的WPF应用程序并添加NuGet包:Caliburn.Micro 2. 删除项目自带的主窗口文件MainWindow.xaml 3. 在App.xaml项目文件中,删 ...