Skip to content

Learning to use #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KeithBarrows opened this issue Jan 7, 2016 · 5 comments
Open

Learning to use #3

KeithBarrows opened this issue Jan 7, 2016 · 5 comments

Comments

@KeithBarrows
Copy link

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

sgryphon commented Aug 3, 2017

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
Copy link
Author

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

@davidchencloudexceed
Copy link

davidchencloudexceed commented Jan 9, 2018

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

jotabe-net added a commit to jotabe-net/SerilogTraceListener that referenced this issue Aug 18, 2020
nblumhardt added a commit that referenced this issue Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants