borisermakof / serilog-sinks-fluentd

A Sink that writes logs into Fluentd
Apache License 2.0
20 stars 31 forks source link

Doesn't work on non-windows. #18

Open dsielert opened 4 years ago

dsielert commented 4 years ago

Preface. I'm not a .NET developer but our devs are trying to use this library on some hosts that are .NET Core on both windows and Linux (Docker) based.. works fine on windows. same code doesn't work at all on Linux. No output is received by fluentd. Nothing is received by the fluentd server.. Even tried to send to a local listening TCP socket and nothing..

dsielert commented 4 years ago

Test case

using System;
using System.Threading;
using Serilog;
using Serilog.Sinks.Fluentd;
using Microsoft.Extensions.Hosting;
using System.IO;
using Microsoft.Extensions.Configuration;
using Serilog.Context;
using System.Runtime.CompilerServices;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using System.Diagnostics;
using System.Linq;

namespace fluent_POC
{
    class CallerEnricher : ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            var skip = 3;
            while (true)
            {
                var stack = new StackFrame(skip);
                if (!stack.HasMethod())
                {
                    logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue("<unknown method>")));
                    return;
                }

                var method = stack.GetMethod();
                if (method.DeclaringType.Assembly != typeof(Log).Assembly)
                {
                    var caller = $"{method.DeclaringType.FullName}.{method.Name}({string.Join(", ", method.GetParameters().Select(pi => pi.ParameterType.FullName))})";
                    logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue(caller)));
                }

                skip++;
            }
        }
    }

    static class LoggerCallerEnrichmentConfiguration
    {
        public static LoggerConfiguration WithCaller(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            return enrichmentConfiguration.With<CallerEnricher>();
        }
    }
    class Program
    {
        private readonly IConfiguration Configuration;
        static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .Build();

            }).ConfigureLogging((hostingContext, builder) =>
            {
                var loggerConfig = new LoggerConfiguration().WriteTo.Console();
                var logconfig = hostingContext.Configuration.GetSection("logging");
                if (logconfig["host"] != "" && logconfig["port"] != "")
                {
                    Console.WriteLine("Configuring Remote Logging...");
                    var host = logconfig["host"];
                    var port = Convert.ToInt32(logconfig["port"]);
                    var tag = logconfig["tag"];
                    loggerConfig.WriteTo.Fluentd(host, port, tag);
                    loggerConfig.Enrich.FromLogContext();
                    loggerConfig.Enrich.WithCaller();
                }

                var log = loggerConfig.CreateLogger();
                while (true)
                {
                   // LogContext.PushProperty("Classname", "bar");
                    log.Information($"That's it!");

                    string custom = "test";
                    int myInt = 42;
                    var macInfo = new { result = true, mac = "norididan",otherVar ="foobar" };
                    var responseTime = 999;

                    //log.Information("Processed Message in {Elapsed:000} ms.", macInfo, responseTime);

                    Thread.Sleep(2000);
                }
            });

    }
}