• Home
  • Privacy Policy
Breaking News, US News, World News and Bollywood News
  • Home
  • Technology
    India opens up its digital payments systems and other key tech

    India opens up its digital payments systems and other key tech

    How to nail your cloud computing job interview

    How to nail your cloud computing job interview

    Lego embraces modularity, metaverse with its software engineering culture

    Lego embraces modularity, metaverse with its software engineering culture

    5 tips for writing better API documentation

    5 tips for writing better API documentation

    What happens when there’s not enough cloud?

    What happens when there’s not enough cloud?

    Identity, trust, and their role in modern applications

    Identity, trust, and their role in modern applications

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

    Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

    Wordle 382 July 6 daily hints: Can’t solve today’s Wordle? Three clues to help with answer | Gaming | Entertainment

    Wordle 382 July 6 daily hints: Can’t solve today’s Wordle? Three clues to help with answer | Gaming | Entertainment

    Fortnite down: Update 21.20 server maintenance schedule – how long is Fortnite downtime? | Gaming | Entertainment

    Fortnite down: Update 21.20 server maintenance schedule – how long is Fortnite downtime? | Gaming | Entertainment

    Avatar 2: ‘Shut the f*** up!’ James Cameron blasts trolls and whining at upcoming sequels | Films | Entertainment

    Avatar 2: ‘Shut the f*** up!’ James Cameron blasts trolls and whining at upcoming sequels | Films | Entertainment

    Xbox Games with Gold ditches Xbox 360 titles in latest blow to subscribers | Gaming | Entertainment

    Xbox Games with Gold ditches Xbox 360 titles in latest blow to subscribers | Gaming | Entertainment

    Next James Bond: Stranger Things star swoops in to take on Tom Hardy | Films | Entertainment

    Next James Bond: Stranger Things star swoops in to take on Tom Hardy | Films | Entertainment

    Trending Tags

      • Bollywood
      • Hollywood
      • Music
    • Lifestyle
      Monty Don: How to prune rambling roses now to encourage ‘more flowers’ next year

      Monty Don: How to prune rambling roses now to encourage ‘more flowers’ next year

      Gardening guru shares how to get an ‘unlimited’ supply of herbs with ‘super easy’ steps

      Gardening guru shares how to get an ‘unlimited’ supply of herbs with ‘super easy’ steps

      Gardening: Heatwave weather forecast warning – how to water your lawn and garden

      Gardening: Heatwave weather forecast warning – how to water your lawn and garden

      Derbyshire allotment holders in desperate effort to dodge eviction

      Derbyshire allotment holders in desperate effort to dodge eviction

      Gardening: ‘Common’ lawn mistakes to avoid this summer – ‘detrimental to its health’

      Gardening: ‘Common’ lawn mistakes to avoid this summer – ‘detrimental to its health’

      Garden: How to paint your decking ‘to guarantee the best finish possible’ – ‘saves time’

      Garden: How to paint your decking ‘to guarantee the best finish possible’ – ‘saves time’

      Trending Tags

      • Golden Globes
      • Game of Thrones
      • MotoGP 2017
      • eSports
      • Fashion Week
    • Nature
    • Business
    • Health
      • Food
    • Fashion
    • Science
    • Sports
    • Travel
    • World News
    No Result
    View All Result
    • Home
    • Technology
      India opens up its digital payments systems and other key tech

      India opens up its digital payments systems and other key tech

      How to nail your cloud computing job interview

      How to nail your cloud computing job interview

      Lego embraces modularity, metaverse with its software engineering culture

      Lego embraces modularity, metaverse with its software engineering culture

      5 tips for writing better API documentation

      5 tips for writing better API documentation

      What happens when there’s not enough cloud?

      What happens when there’s not enough cloud?

      Identity, trust, and their role in modern applications

      Identity, trust, and their role in modern applications

      Trending Tags

      • Nintendo Switch
      • CES 2017
      • Playstation 4 Pro
      • Mark Zuckerberg
    • Entertainment
      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Wordle 382 July 6 daily hints: Can’t solve today’s Wordle? Three clues to help with answer | Gaming | Entertainment

      Wordle 382 July 6 daily hints: Can’t solve today’s Wordle? Three clues to help with answer | Gaming | Entertainment

      Fortnite down: Update 21.20 server maintenance schedule – how long is Fortnite downtime? | Gaming | Entertainment

      Fortnite down: Update 21.20 server maintenance schedule – how long is Fortnite downtime? | Gaming | Entertainment

      Avatar 2: ‘Shut the f*** up!’ James Cameron blasts trolls and whining at upcoming sequels | Films | Entertainment

      Avatar 2: ‘Shut the f*** up!’ James Cameron blasts trolls and whining at upcoming sequels | Films | Entertainment

      Xbox Games with Gold ditches Xbox 360 titles in latest blow to subscribers | Gaming | Entertainment

      Xbox Games with Gold ditches Xbox 360 titles in latest blow to subscribers | Gaming | Entertainment

      Next James Bond: Stranger Things star swoops in to take on Tom Hardy | Films | Entertainment

      Next James Bond: Stranger Things star swoops in to take on Tom Hardy | Films | Entertainment

      Trending Tags

        • Bollywood
        • Hollywood
        • Music
      • Lifestyle
        Monty Don: How to prune rambling roses now to encourage ‘more flowers’ next year

        Monty Don: How to prune rambling roses now to encourage ‘more flowers’ next year

        Gardening guru shares how to get an ‘unlimited’ supply of herbs with ‘super easy’ steps

        Gardening guru shares how to get an ‘unlimited’ supply of herbs with ‘super easy’ steps

        Gardening: Heatwave weather forecast warning – how to water your lawn and garden

        Gardening: Heatwave weather forecast warning – how to water your lawn and garden

        Derbyshire allotment holders in desperate effort to dodge eviction

        Derbyshire allotment holders in desperate effort to dodge eviction

        Gardening: ‘Common’ lawn mistakes to avoid this summer – ‘detrimental to its health’

        Gardening: ‘Common’ lawn mistakes to avoid this summer – ‘detrimental to its health’

        Garden: How to paint your decking ‘to guarantee the best finish possible’ – ‘saves time’

        Garden: How to paint your decking ‘to guarantee the best finish possible’ – ‘saves time’

        Trending Tags

        • Golden Globes
        • Game of Thrones
        • MotoGP 2017
        • eSports
        • Fashion Week
      • Nature
      • Business
      • Health
        • Food
      • Fashion
      • Science
      • Sports
      • Travel
      • World News
      No Result
      View All Result
      Updates News
      No Result
      View All Result
      Home Technology

      How to work with trace listeners in ASP.NET Core 6

      admin by admin
      June 23, 2022
      in Technology
      0
      How to work with trace listeners in ASP.NET Core 6
      0
      SHARES
      7
      VIEWS
      Share on FacebookShare on Twitter


      When working on applications built using ASP.NET Core 6, you might often want to use tracing and logging to monitor your application’s performance and to diagnose errors. You can also use tracing in a production environment to measure how your application is performing at run time.

      This article discusses how we can use tracing in ASP.NET Core 6. We will examine how to use trace listeners to collect trace messages and direct the trace output to an event log using ILogger.

      To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

      Create an ASP.NET Core Web API project in Visual Studio 2022

      First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core 6 Web API project in Visual Studio 2022:

      1. Launch the Visual Studio 2022 IDE.
      2. Click on “Create new project.”
      3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
      4. Click Next.
      5. In the “Configure your new project” window, specify the name and location for the new project.
      6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
      7. Click Next.
      8. In the “Additional Information” window shown next, ensure that the checkbox that says “Use controllers…” is checked since we’ll not be using minimal APIs in this example. Leave the “Authentication Type” as “None” (default).
      9. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be using any of those features here.
      10. Click Create.

      We’ll use this ASP.NET Core 6 Web API project to work with trace listeners in the subsequent sections of this article.

      What is tracing?

      Compared to event logging, which tracks major events, tracing allows for a much more complete view of the running application and its components. Logs comprise structured or unstructured time stamped data that shows a record of the events that occur in your application. Tracing provides much more visibility into the individual request and how it is processed.

      The System.Diagnostics namespace contains the Trace and the Debug classes. While the Trace class is used in production environments, the Debug class is used at development time.

      Tracing typically involves the following three phases:

      • Instrumentation: We write the necessary code to capture relevant information
      • Tracing: We write the trace messages to a specified target, i.e., an event log, a text file, a database table, etc.
      • Analysis: We analyze the information gathered from traces to determine the bottlenecks in the application.

      What are trace listeners? Why are they needed?

      Trace listeners collect trace messages, store them, and direct them to an appropriate target such as a text file. .NET provides several trace listeners including the following:

      • ConsoleTraceListener – sends trace messages to the console window.
      • DefaultTraceListener – sends trace messages to standard debug output.
      • DelimitedListTraceListener – sends trace output in a delimited format to a stream, a stream writer, or a text writer.
      • EventLogTraceListener – sends trace messages to event logs.
      • TextWriterTraceListener – sends trace messages to a text file.
      • XmlWriterTraceListener – converts trace messages to XML.

      The System.Diagnostics.Debug and System.Diagnostics.Trace classes can send messages to trace listeners, which in turn route the messages to an appropriate target.

      Create a trace listener using a config file in ASP.NET Core 6

      You can create a trace listener either by using a config file or by writing custom code. The code snippet shown below illustrates how to create a trace listener using your application configuration file.

      <configuration>
        <system.diagnostics>
          <trace autoflush="false" indentsize="4">
            <listeners>
              <add name="MyFirstListener"
              type="System.Diagnostics.TextWriterTraceListener"
              initializeData="TraceOutput.txt" />
              <remove name="Default" />
            </listeners>
          </trace>
        </system.diagnostics>
      </configuration>

      All listeners added to the Listeners collection will receive trace output. However, you can use a listener without adding it to the Listeners collection. In this case, you send output using the Write or WriteLine method within the listener.

      The following code illustrates a listener that is not added to the Listeners collection but is still capable of sending trace messages to an output window, a file, or any pre-configured output.

      TextWriterTraceListener myFirstListener = new
      TextWriterTraceListener("Output.txt", "myFirstListener");
      myFirstListener.WriteLine("This is a test message.");
      myFirstListener.Flush();

      Create a custom trace listener in ASP.NET Core 6

      The trace listeners that come with .NET 6 by default will meet your requirements in most cases. However, if you want to output your trace messages to a different destination, you can implement your own trace listener.

      To build a custom trace listener, you should create a class that extends the TraceListener abstract class. There are several virtual and abstract methods in the TraceListener class. You should at least implement the Write and the WriteLine methods. At a bare minimum, your custom trace listener should look like this:

      public class CustomTraceListener : TraceListener
      {
          public CustomTraceListener(ILoggerFactory loggerFactory)
          {
          }
          public override void Write(string? message, string? category)
          {           
          }  
          public override void Write(string? message)
          {           
          }
          public override void WriteLine(string? message)
          {           
          }
      }

      So, your custom trace listener class must have an argument constructor and the Write and WriteLine methods.

      You will also need an ILogger instance that represents the logger, a logger factory to create the logger, and a StringBuilder to store the trace messages before they are sent to the log target. 

      private readonly ILoggerFactory _loggerFactory;
      private readonly ILogger _iLogger;
      private readonly StringBuilder _stringBuilder = new();

      You can take advantage of dependency injection to inject an instance of ILoggerFactory in the constructor and then use the instance to create an instance of ILogger.

      public CustomTraceListener(ILoggerFactory loggerFactory)
      {
         _loggerFactory = loggerFactory;
         _iLogger = loggerFactory.CreateLogger(nameof(CustomTraceListener));
      }

      Here is a minimal implementation of the Write and the WriteLine methods:

      public override void Write(string? message, string? category)
      {
           _stringBuilder.Append(message + "-" + category);
      }
      public override void Write(string? message)
      {
           _stringBuilder.Append(message);
      }
      public override void WriteLine(string? message)
      {
         _stringBuilder.AppendLine(message);
         _iLogger.LogInformation(_stringBuilder.ToString());
         _stringBuilder.Clear();
      }

      Complete custom trace listener example in ASP.NET Core 6

      Below is the complete source code of our minimal implementation of a custom trace listener for your reference.

      using System.Collections.Concurrent;
      using System.Diagnostics;
      using System.Text;
      namespace TraceListenerDemo
      {
          public class CustomTraceListener : TraceListener
          {
              private readonly ILoggerFactory _loggerFactory;
              private readonly ILogger _iLogger;
              private readonly StringBuilder _stringBuilder = new();
              public CustomTraceListener(ILoggerFactory loggerFactory)
              {
                  _loggerFactory = loggerFactory;
                  _iLogger =
                   loggerFactory.CreateLogger(nameof(CustomTraceListener));
              }
              public override void Write(string? message, string? category)
              {
                  _stringBuilder.Append(message + "-" + category);
              }
              public override void Write(string? message)
              {
                  _stringBuilder.Append(message);
              }
              public override void WriteLine(string? message)
              {
                  _stringBuilder.AppendLine(message);
                  _iLogger.LogInformation(_stringBuilder.ToString());
                  _stringBuilder.Clear();
              }
          }
      }

      Register the custom trace listener in the Program.cs file

      To use the custom trace listener, you should register it with the Listeners collection using the following code.

      var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
      Trace.Listeners.Add(new LoggerTraceListener(loggerFactory));

      Because our custom trace listener has been added to the listeners collection, it will capture all trace messages generated by the runtime and send the output to our logger. It will also send any trace messages that we send explicitly in the application (like we did in the myFirstListener example earlier).

      So, any listener added to the Listeners collection can capture the traces generated by the runtime as well as any trace messages sent explicitly in the application. However, if a trace listener is not added to the collection, it can only send trace messages sent explicitly in the application. It will not capture any trace messages generated by the runtime.

      When working with custom trace listeners, you must remember to close or flush the trace listener to ensure that the output buffer is emptied. You can take advantage of the StringBuilderCache class to optimize your code (in the CustomTraceListener class) that uses StringBuilder.

      Copyright © 2022 IDG Communications, Inc.



      Source link

      admin

      admin

      • Trending
      • Comments
      • Latest
      UK’s most dangerous plant: Father’s warning — ‘Never seen a child so badly burned’ | Science | News

      UK’s most dangerous plant: Father’s warning — ‘Never seen a child so badly burned’ | Science | News

      June 6, 2022
      Plant warning as bamboo causes £100,000 of damage to Hampshire property

      Plant warning as bamboo causes £100,000 of damage to Hampshire property

      January 31, 2022
      Social workers warned weeks before baby Mitchell died in 2019 | TV & Radio | Showbiz & TV

      Social workers warned weeks before baby Mitchell died in 2019 | TV & Radio | Showbiz & TV

      January 2, 2022
      Yorkshire nan on how to make Yorkshire puddings with no ingredients weighed

      Yorkshire nan on how to make Yorkshire puddings with no ingredients weighed

      February 15, 2022
      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      0
      Boris Johnson slammed by Indie band for using their song ‘Blue Bunch Of Corrupt W****rs’ | Music | Entertainment

      Boris Johnson slammed by Indie band for using their song ‘Blue Bunch Of Corrupt W****rs’ | Music | Entertainment

      0
      Call Your Mom, Because Sue Grafton’s Alphabet Murder Mystery Books Are Becoming A Show

      Call Your Mom, Because Sue Grafton’s Alphabet Murder Mystery Books Are Becoming A Show

      0
      Release Date, Cast, And More

      Release Date, Cast, And More

      0
      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      July 6, 2022
      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      July 6, 2022
      Kate Middleton Keeps Wearing Princess Diana’s Favorite Print

      Kate Middleton Keeps Wearing Princess Diana’s Favorite Print

      July 6, 2022
      Kate Middleton: Royal dazzles in blue and white dress at Wimbledon – pictures

      Kate Middleton: Royal dazzles in blue and white dress at Wimbledon – pictures

      July 6, 2022

      Recent News

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      July 6, 2022
      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      July 6, 2022
      Kate Middleton Keeps Wearing Princess Diana’s Favorite Print

      Kate Middleton Keeps Wearing Princess Diana’s Favorite Print

      July 6, 2022
      Kate Middleton: Royal dazzles in blue and white dress at Wimbledon – pictures

      Kate Middleton: Royal dazzles in blue and white dress at Wimbledon – pictures

      July 6, 2022
      Breaking News, US News, World News and Bollywood News

      Follow Us

      Browse by Category

      • Bollywood
      • Business
      • Entertainment
      • Fashion
      • Food
      • Health
      • Hollywood
      • Lifestyle
      • Music
      • Nature
      • Science
      • Sports
      • Technology
      • Travel
      • World News

      Recent News

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      Daily Quordle 163 hints for July 6 – Clues to help solve today’s tricky Quordle puzzle | Gaming | Entertainment

      July 6, 2022
      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      China testing Feitian-1 hypersonic missile with transforming engine | Science | News

      July 6, 2022
      • Home
      • Privacy Policy

      © 2021 Updates News

      No Result
      View All Result

      © 2021 Updates News