serilog-contrib / serilog-diagnostics-tracelistener

A System.Diagnostics.TraceListener that writes trace data into Serilog
Apache License 2.0
25 stars 14 forks source link

Learning to use #3

Open KeithBarrows opened 8 years ago

KeithBarrows commented 8 years ago

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 commented 8 years ago

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);
}
nblumhardt commented 8 years ago

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

sgryphon commented 7 years ago

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>
KeithBarrows commented 7 years ago

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

davidchencloudexceed commented 6 years ago

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