. NET use of SEQ structured log system

Source: Internet
Author: User

Preface

Our company in the journal Management has not been unified, the main pain points are:

    1. Each developer is used each, the form of storage logs is also various, such as: Local files, databases, Redis,mongodb
    2. Because the company accesses the server through the bastion machine, the machine cannot directly connect to the test or production environment. If the test log is written to a local file, it is very inconvenient for developers to be able to log on to the server only to view the logs.

There are three options as described above:

        1. Write a log system yourself
        2. Find a ready-made
        3. No logs are logged.

Comprehensive consideration, the 3rd is certainly not reliable, the 1th cost is too high, the company is relatively busy, then only to find a ready-made ...

about SEQ

SEQ is a log server that runs on a single machine. Your application sends structured events through a framework like Serilog:

Log.information ("Hello, {name}! ", Environment.username);

Structured logging retains the custom property values and the text associated with each event.

This information is sent over the network to Seq,seq to display them and make them searchable:

Currently supported in three languages:. NET and net Core,java,node.js, as well as Python.

Seq Main Features:

Rich event data -records the above information and application events in a structured form, supporting message templates to seamlessly connect text data to structured session data. All of the properties in the are custom-defined by the consumer.

multiple Query modes -support for SQL expression queries, and C #, ==,!=,<,<=, built-in StartsWith (), EndsWith() IndexOf() and Contains(),并支持文本全文检索。

仪表盘 - 搜索框添加查询语句也可以是SQL查询,匹配到相应的数据后,使用右侧的Add to dashboard创建对应的仪表盘

添加警报 -当检测到警报情况时,可以通过邮件将情况通知到相关人员。单击仪表板图表上ALERTS选项卡下的+按钮:

 

      Seq缺点:

  1. 内存消耗较高,属于那种有多少就吃多少,如果吃撑了,就又吐出来一部分返回给系统,真的是有点不要脸。
  2. 收费,非开源系统,所以免费版本功能会有阉割。

Detailed information can be viewed on official website documents: Https://docs.getseq.net/docs

. NET Core Practices

1. Download and install the SEQ server

2. Open the Setup program to start the Installation wizard, all the way to the next step, after completing the browser input http://localhost:5341/#/events,5341 is the default port of Seq. The SEQ is hosted in a Windows service and is viewed by running the win+r input services.msc command.

Once the environment has been built, let's look at how to apply it in the project:

1. Create a new project with ASP. NET Core or MVC can

2. Add the NuGet package:

              • Seq.Extensions.Logging
              • Autofac
              • Autofac.Extensions.DependencyInjection

   3.在appsettings.json添加一个"Seq"属性:

{"  Logging": {    "includescopes": false,    "Debug": {      "LogLevel": {        "Default": "Warning"      }    },    "Seq": {      "ServerURL": "http://localhost:5341/",//SEQ Local environment address.      " ApiKey ":" FCT0LNQSPU3KITUQMB ",//The project name created in the SEQ System key value. This key value can be used to retrieve all the log information under the item      "Minimumlevel": "Trace",      "Leveloverride": {        "Microsoft": "Warning"      }    },    "Console": {      "LogLevel": {        "Default": "Warning"      }  }}}

4. In the Configureservices () method of your startup class, call Addseq () on Loggingbuilder to provide addlogging ().

Services. Addlogging (Loggingbuilder =            {                //loggingbuilder.addseq ();                Loggingbuilder.addseq (Configuration.getsection ("Seq");            });

5. Configuring Dependency Injection

 PublicIServiceProvider configureservices (iservicecollection services) {services. Addlogging (Loggingbuilder=            {                /*This method uses the local address by default: Port 5341*/                //loggingbuilder.addseq ();                /*Load SEQ Configuration*/Loggingbuilder.addseq (Configuration.getsection ("Seq"));            }); Services.            Addmvc (); /*this uses AUTOFAC as a dependency injection*/            varContainerbuilder =NewContainerbuilder (); /*inject SEQ record log Object*/Containerbuilder.registergeneric (typeof(logger<>)). As (typeof(ilogger<>)).            Instancepermatchinglifetimescope (); /*because you changed the default dependency injection framework, you need to populate its original object with values in the new dependency framework*/containerbuilder.populate (services); varcontainer =Containerbuilder.build (); return NewAutofacserviceprovider (container); }

6. Inject the ILogger instance into the controller and write to the log

PrivateIlogger<valuescontroller>_logger;  PublicValuescontroller (ilogger<valuescontroller>logger) {_logger=logger; }        //GET api/values[HttpGet] Publicienumerable<string>Get () {_logger. Loginformation ("{projece}&{modulename} Get Test","Seqexample","Values"); return New string[] {"value1","value2" }; }

7. Running Results

As you can see, we've customized two properties, project and modulename through the message template. In actual use, we can set up a unified message template according to the company's business, classify the project log and view it.

Summarize

Since I am using the free version now, there are many limitations, such as: cannot change the local to the public address (can be resolved by the reverse proxy), does not support mail alerts.

Similar to this limitation, but none of them affect the use, as the log system, the free version of the basic functionality is enough to use, and the query performance is not weak. Who let. NET open source framework is less, I am already satisfied with writing a system like this!

SEQ currently in the domestic use of the company is very few, so this article is only a catalyst, so that you can choose a log frame more than one choice, that's all.

. NET use of SEQ structured log system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.