1. 程式人生 > >採用Opserver來監控你的ASP.NET專案系列(二、監控SQL Server與Asp.Net專案)

採用Opserver來監控你的ASP.NET專案系列(二、監控SQL Server與Asp.Net專案)

原文: 採用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);
  }

這樣,異常也會記錄到儲存裡面去了.

 

寫在最後

本篇到此結束,下篇介紹如何監控我們的伺服器狀態