zoukankan      html  css  js  c++  java
  • 采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)

    之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看.

    今天我们主要来介绍一下,如何使用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”,如下:

           

    <configSections>
        <section name="Exceptional" type="StackExchange.Exceptional.Settings" />
      </configSections>

          

       3.在web.config中增加Exceptional节点,如下:

    <Exceptional applicationName="应用名称">
       
        <!--<ErrorStore type="Memory" />-->
        <!--连接opserver数据库时开启-->
        <ErrorStore type="存储类型" connectionString="连接字符串" />
      </Exceptional>

    ErrorStore 错误存储有4种实现方式,Memory,JSON,SQL,MySQL,如下是官方的说明译文:

        <!--如果没有设置ErrorStore,将默认使用内存的形式来记录错误-->
        <!--<ErrorStore type="Memory" />-->
        <!-- 其他的存储类型, 相关的设置属性如下:
             - rollupSeconds:页面上异常的更新秒数,默认为600秒
             - backupQueueSize: 设置缓存多少错误,默认为1000条-->
        <!-- JSON:Size是设置Json存储的文件数量,默认为200-->
        <!--<ErrorStore type="JSON" path="~/Errors" size="200" />-->
        <!-- SQL: 只需要设置数据库连接字符串如下: -->
        <!--<ErrorStore type="SQL" connectionString="Server=.;Database=Exceptions;Uid=Exceptions;Pwd=myPassword!" />-->
        <!--<ErrorStore type="SQL" connectionStringName="MyConnectionString" />-->
        <!--你也可以设置为Mysql如下 -->
        <!--<ErrorStore type="MySQL" connectionString="Server=.;Database=Exceptions;Username=Exceptions;Pwd=myPassword!" />-->
        <!--<ErrorStore type="MySQL" connectionStringName="MyConnectionString" />-->

    这里我们采用SQL的形式,直接存在数据库里.

    4.修改web.config的system.webServer节点,添加新的handlers,modules配置如下:

    <system.webServer>
     <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <add name="Exceptional" path="exceptions.axd" verb="POST,GET,HEAD" type="StackExchange.Exceptional.HandlerFactory, StackExchange.Exceptional" preCondition="integratedMode" />
        </handlers>
        <modules>
          <add name="ErrorLog" type="StackExchange.Exceptional.ExceptionalModule, StackExchange.Exceptional" />
        </modules>
      </system.webServer>

    5.因为我这里采用的SQL存储,所以需要给数据库添加存储错误信息的表,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

    6.最后回到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 don't crash
                    ErrorStore.LogExceptionWithoutContext(ex);
      }

    这样,异常也会记录到存储里面去了.

    写在最后

    本篇到此结束,下篇介绍如何监控我们的服务器状态

  • 相关阅读:
    JavaScript 字符串常用操作
    Redis分布式锁
    CSS布局之-水平垂直居中
    vuejs学习笔记(1)--属性,事件绑定,ajax
    Angular.js学习笔记 (一)
    几年前写的一个支持多数据库切换的设计
    bundle中vim相关快捷键的使用
    使用Bundle进行VIM插件的管理
    javascript代码在线测试
    线程池的创建
  • 原文地址:https://www.cnblogs.com/GuZhenYin/p/8064860.html
Copyright © 2011-2022 走看看