昆山建设网站,wordpress 主题轮播,云南做网站公司哪家好,1年网站前言之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看.ASP.NET Core之跨平台的实时性能监控ASP.NET Core之跨平台的实时性能监控(2.健康检查)今天我们主要来介绍一下,如何使用Opserver监控我们的SQL Server 和ASP.NET项目的异常监控监控效果如下:SQL Server的… 前言之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看. ASP.NET Core之跨平台的实时性能监控ASP.NET Core之跨平台的实时性能监控(2.健康检查)今天我们主要来介绍一下,如何使用Opserver监控我们的SQL Server 和ASP.NET项目的异常监控 监控效果如下:SQL Server的: ASP.NET异常情况的监控: 监控SQL Server首先我们来讲解一下如何监控我们的SQL Server.上篇内容我们已经提到过 Opeserver的项目有很多的配置文件.我们找到我们的SQLSettings.example.json文件,改名为SQLSettings.json文件修改其中的配置项如下:/* Configuration for the SQL Server dashboard */{ defaultConnectionString: , refreshIntervalSeconds: 30, clusters: [ { name: 192.168.1.120, refreshIntervalSeconds: 20, nodes: [ { name: 192.168.1.121 }, { name: 192.168.1.122 }, { name: 192.168.1.123 } ] } ], instances: [ { name: 实例名称, connectionString: 数据库连接字符串, refreshIntervalSeconds: 200 } ]}解释一下其中的意义,参照如下:defaultConnectionString (默认的连接字符串,用于单台数据库监控)refreshIntervalSeconds (轮询数据库情况的刷新时间,如果不设置,默认为60秒)instances (当有多台单独的数据库实例需要监控时候的数据库实例设置)clusters (当你的数据库是集群部署的时候的设置)后面的内容都一样,我就不一一解释了,多台数据库实例,可以自行在instances 中添加多个节点,集群就在clusters中加入节点地址即可然后,我们直接运行OpSever项目,就可以观察到数据库的变化情况了.监控ASP.NET项目的异常情况 下面我们来讲讲如何监控我们的ASP.NET项目异常的情况 1.我们需要在在web项目中通过nuget安装StackExchange.Exceptional组件它依赖于dapper 2.在web.config中的configSections节点下增加section节点 “Exceptional”,如下: configSectionssection nameExceptional typeStackExchange.Exceptional.Settings //configSections 3.在web.config中增加Exceptional节点,如下:Exceptional applicationName应用名称 !--ErrorStore typeMemory /-- !--连接opserver数据库时开启-- ErrorStore type存储类型 connectionString连接字符串 / /ExceptionalErrorStore 错误存储有4种实现方式Memory,JSON,SQL,MySQL,如下是官方的说明译文:!--如果没有设置ErrorStore,将默认使用内存的形式来记录错误-- !--ErrorStore typeMemory /-- !-- 其他的存储类型, 相关的设置属性如下: - rollupSeconds:页面上异常的更新秒数,默认为600秒 - backupQueueSize: 设置缓存多少错误,默认为1000条-- !-- JSON:Size是设置Json存储的文件数量,默认为200-- !--ErrorStore typeJSON path~/Errors size200 /-- !-- SQL: 只需要设置数据库连接字符串如下: -- !--ErrorStore typeSQL connectionStringServer.;DatabaseExceptions;UidExceptions;PwdmyPassword! /-- !--ErrorStore typeSQL connectionStringNameMyConnectionString /-- !--你也可以设置为Mysql如下 -- !--ErrorStore typeMySQL connectionStringServer.;DatabaseExceptions;UsernameExceptions;PwdmyPassword! /-- !--ErrorStore typeMySQL connectionStringNameMyConnectionString /--这里我们采用SQL的形式,直接存在数据库里.4.修改web.config的system.webServer节点,添加新的handlers,modules配置如下:system.webServer validation validateIntegratedModeConfigurationfalse / handlers add nameExceptional pathexceptions.axd verbPOST,GET,HEAD typeStackExchange.Exceptional.HandlerFactory, StackExchange.Exceptional preConditionintegratedMode / /handlers modules add nameErrorLog typeStackExchange.Exceptional.ExceptionalModule, StackExchange.Exceptional / /modules /system.webServer5.因为我这里采用的SQL存储,所以需要给数据库添加存储错误信息的表,SQL语句如下:USE [OpServerTest]GO/****** Object: Table [dbo].[Exceptions] Script Date: 2016/11/16 16:28:56 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE 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]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[Exceptions] ADD DEFAULT ((0)) FOR [IsProtected]GOALTER TABLE [dbo].[Exceptions] ADD DEFAULT ((1)) FOR [DuplicateCount]GO6.最后回到OpServer项目修改ExceptionsSettings.example.json文件为ExceptionsSettings.json,并添加配置如下:{ stores: [ //异常日志存储位置 { name: ExceptionDB, queryTimeoutMs: 2000, pollIntervalSeconds: 10, connectionString: 错误存储的地址 } ]}7.想增加自定义的错误信息,可以编写如下代码:try { throw new Exception(Just a try/catch test); } catch (Exception ex) { // logged, but caught so we dont crash ErrorStore.LogExceptionWithoutContext(ex); }这样,异常也会记录到存储里面去了. 写在最后本篇到此结束,下篇介绍如何监控我们的服务器状态原文http://www.cnblogs.com/GuZhenYin/p/8064860.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com