Serilogs | How to turn off Blazor http calls?

58 views Asked by At

I am trying to turn off all the logs except the logs I put in directly in my application.

I see a lot of http post and get calls from Blazor.

How can I ignore those?

HTTP "POST" "/_blazor/negotiate" responded 200 in 0.2372 ms
HTTP "GET" "/_blazor/initializers" responded 200 in 0.1806 ms
HTTP "GET" "/_framework/blazor.web.js" responded 304 in 0.0920 ms
HTTP "POST" "/_blazor/disconnect" responded 200 in 0.6568 ms
HTTP "GET" "/_blazor" responded 101 in 208.2413 ms
HTTP "GET" "/Account/Login" responded 200 in 7.2417 ms
HTTP "GET" "/" responded 200 in 2.4507 ms
HTTP "POST" "/_blazor/negotiate" responded 200 in 0.3406 ms
HTTP "POST" "/_blazor/negotiate" responded 200 in 0.2164 ms
HTTP "GET" "/_blazor/initializers" responded 200 in 0.1783 ms
HTTP "GET" "/_framework/blazor.web.js" responded 304 in 0.0720 ms

Here is my current appsettings.json file:

    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Warning",
        "Microsoft.AspNetCore.Hosting.Diagnostics": "Warning",
        "Microsoft.AspNetCore.Mvc.RazorPages": "Warning",
        "Microsoft.AspNetCore.Mvc.ViewFeatures": "Warning",
        "Microsoft.AspNetCore.StaticFiles": "Warning",
        // The migration is not applied, so what?
        "Microsoft.EntityFrameworkCore.Migrations": "Warning",
        // DbCommand executed, so what?
        "Microsoft.EntityFrameworkCore.Database": "Warning",
        "Microsoft.AspNetCore.Mvc.Infrastructure": "Warning"
      }
    },
1

There are 1 answers

0
tvbRPTI2754 On

In Program.cs

        //Add support to logging with SERILOG
        builder.Host.UseSerilog((context, configuration) =>
        {
            configuration
                .ReadFrom.Configuration(context.Configuration)
                .Filter.ByExcluding(evt =>
                {
                    var path = evt.Properties.ContainsKey("RequestPath") ? evt.Properties["RequestPath"].ToString().Replace("\"", string.Empty) : "";
                    var method = evt.Properties.ContainsKey("RequestMethod") ? evt.Properties["RequestMethod"].ToString().Replace("\"", string.Empty) : "";
                    var statusCode = evt.Properties.ContainsKey("StatusCode") ? int.Parse(evt.Properties["StatusCode"].ToString()) : 0;

                    return (path == "/_blazor/disconnect" && method == "POST") ||
                           (path == "/_blazor/negotiate" && method == "POST") ||
                           (path == "/_blazor/initializers" && method == "GET") ||
                           (path == "/_framework/blazor.web.js" && method == "GET") ||
                           (path == "/_blazor" && method == "GET") ||
                           (path == "/" && method == "GET");
                });
        });

Full appsetting.json for Serilog

{
  "ConnectionStrings": {
    "DefaultConnection": ""
  },
  "BaseAddress": "",
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Warning",
        "Microsoft.AspNetCore.Hosting.Diagnostics": "Warning",
        "Microsoft.AspNetCore.Mvc.RazorPages": "Warning",
        "Microsoft.AspNetCore.Mvc.ViewFeatures": "Warning",
        "Microsoft.AspNetCore.StaticFiles": "Warning",
        // The migration is not applied, so what?
        "Microsoft.EntityFrameworkCore.Migrations": "Warning",
        // DbCommand executed, so what?
        "Microsoft.EntityFrameworkCore.Database": "Warning",
        "Microsoft.AspNetCore.Mvc.Infrastructure": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "Serilogs/applog-.txt",
          "rollingInterval": "Day"
        }
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "DefaultConnection",
          "sinkOptionsSection": {
            "tableName": "Serilogs",
            "schemaName": "dbo",
            "autoCreateSqlTable": false,
            "batchPostingLimit": 1000,
            "period": "0.00:00:30"
          },
          "restrictedToMinimumLevel": "Information",
          "columnOptionsSection": {
            "disableTriggers": false,
            "clusteredColumnstoreIndex": false,
            "primaryKeyColumnName": "Id",
            "addStandardColumns": [ "LogEvent", "TraceId", "SpanId" ],
            //"removeStandardColumns": [ "MessageTemplate", "Properties" ],
            "additionalColumns": [
              {
                "ColumnName": "AspNetUsersId",
                "DataLength": 450,
                "DataType": "nvarchar"
              },
              {
                "ColumnName": "EnvName",
                "DataLength": 450,
                "DataType": "nvarchar"
              }
            ],
            "id": {
              "nonClusteredIndex": true,
              "columnName": "Id"
            },
            "level": {
              "columnName": "Level",
              "storeAsEnum": false
            },
            "properties": {
              "columnName": "Properties",
              "excludeAdditionalProperties": false,
              "dictionaryElementName": "dict",
              "itemElementName": "item",
              "omitDictionaryContainerElement": false,
              "omitSequenceContainerElement": false,
              "omitStructureContainerElement": false,
              "omitElementIfEmpty": true,
              "propertyElementName": "prop",
              "rootElementName": "root",
              "sequenceElementName": "seq",
              "structureElementName": "struct",
              "usePropertyKeyAsElementName": false
            },
            "timeStamp": {
              "columnName": "TimeStamp",
              "convertToUtc": true
            },
            "logEvent": {
              "excludeAdditionalProperties": false,
              "excludeStandardColumns": false,
              "columnName": "LogEvent"
            },
            "message": { "columnName": "Message" },
            "exception": { "columnName": "Exception" },
            "messageTemplate": { "columnName": "MessageTemplate" }
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Properties": {
      "ApplicationName": "App Name"
    }
  }
}