serilog-diagnostics-tracelistener icon indicating copy to clipboard operation
serilog-diagnostics-tracelistener copied to clipboard

Learning to use

Open KeithBarrows opened this issue 9 years ago • 5 comments

Thanks for the library. Now, how do we properly use it? I've barely ever used trace and am wondering if it should be injected into the Trace.Listeners?

Scenario: A Nuget contains tracing to a file. I would like to capture that and send, via Serilog, to Seq, my file, etc. Or do I need access to the code (the nuget package) and override the trace there? Or something different?

KeithBarrows avatar Jan 07 '16 22:01 KeithBarrows

As additional info, looking through the NuGet code base (of the nuget I want to include, not your nuget) I see one place, and one place only that Trace is used. A Tracing class has been created that puts a facade on the different levels of events, The rest of the code then calls like Tracing.Verbose("..."); The facade then passes the message and eventtype to the following method inside the Tracing class:

public static void TraceEvent(TraceEventType type, string message)
{
     // Original code...
    TraceSource ts = new TraceSource("The Nuget Name");
    if (Trace.CorrelationManager.ActivityId == Guid.Empty)
    {
        if (type != TraceEventType.Verbose)
        {
            Trace.CorrelationManager.ActivityId = Guid.NewGuid();
        }
    }
    ts.TraceEvent(type, 0, message);

    // What I *think* should go here...
    if (_traceEventCache == null)
        _traceEventCache = new TraceEventCache();
    if (Serilog.Log.Logger != null && _traceListener == null)
        _traceListener = new global::SerilogTraceListener.SerilogTraceListener(Log.Logger);
    _traceListener.TraceEvent(_traceEventCache, "<<NugetName>>", type, 0, message);
}

KeithBarrows avatar Jan 07 '16 23:01 KeithBarrows

Hi Keith,

The author of this project has some other commitments but invited me to help keep the wheels turning here, which so far I've failed miserably to do :-) ... but I'm picking things up now.

Do you still need help with this? I've added what information I have to the README, it'd be great to know if anything is missing.

Regards, Nick

nblumhardt avatar Apr 22 '16 00:04 nblumhardt

Example program:

using Serilog;
using System;
using System.Diagnostics;
namespace Example.SerilogTrace
{
    // PM> Install-Package SerilogTraceListener
    // PM> Install-Package Serilog.Sinks.Literate
    public class Program
    {
        static void Main(string[] args)
        {
            // Initialise serilog (can also be done via appSettings in config file)
            Log.Logger = new LoggerConfiguration()
                .WriteTo.LiterateConsole()
                .CreateLogger();
            // Set correlation id
            Trace.CorrelationManager.ActivityId = Guid.NewGuid();
            // Create a source
            var source = new TraceSource("Example.Source.Name");
            // Trace an event
            source.TraceEvent(TraceEventType.Information, 1001, "level={0} os={1}", 5, Environment.OSVersion);        
            Console.ReadLine();
        }
    }
}

With config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.diagnostics>
    <sharedListeners>
      <add name="serilog" type="SerilogTraceListener.SerilogTraceListener, SerilogTraceListener" />
    </sharedListeners>
    <sources>
      <source name="Example.Source.Name" switchValue="All">
        <listeners>
          <clear/>
          <add name="serilog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

sgryphon avatar Aug 03 '17 12:08 sgryphon

Getting caught back up, and 2 clients later. I will play with this over the next 10 days and see how it goes.

KeithBarrows avatar Aug 03 '17 17:08 KeithBarrows

Hello, I tried the sample but it only output the string. It doesn't treat it as MessageTemplate. So it lost the most powerful feature of serilog which is structured log. see image below

image

I tried source.TraceEvent(TraceEventType.Information, 1001, "hello world {level} {os}", 5, Environment.OSVersion);

Still no luck image

davidchencloudexceed avatar Jan 09 '18 18:01 davidchencloudexceed