serilog-contrib / serilog-sinks-elasticsearch

A Serilog sink that writes events to Elasticsearch
Apache License 2.0
434 stars 197 forks source link

Logger send elastic one value at a time #319

Open Lucifer-Elf opened 4 years ago

Lucifer-Elf commented 4 years ago

A few questions before you begin:

Is this an issue related to the Serilog core project or one of the sinks or community projects.
This issue list is intended for Serilog Elasticsearch Sink issues. If this issue relates to another sink or to the code project, please log on the related repository. Please use Gitter chat and Stack Overflow for discussions and questons.

Does this issue relate to a new feature or an existing bug?

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package.

What is the target framework and operating system? See target frameworks & net standard matrix.

Please describe the current behavior?

Please describe the expected behavior?

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem

Lucifer-Elf commented 4 years ago

public Logger() {
_loggerConfiguration = new LoggerConfiguration(); _loggerConfiguration.Enrich.WithMachineName().Enrich.WithProcessId().Enrich.WithThreadId() .MinimumLevel.Information();

    }

    public static void CreateLogger()
    {
        Log.Logger =_loggerConfiguration.CreateLogger();

    }

    public static void EnableElasticSearch(string url = "")
    {
        _loggerConfiguration.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/"))
          {
            AutoRegisterTemplate = true,
            AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
            IndexFormat = "serilog",
            CustomFormatter = new ExceptionAsObjectJsonFormatter(),

        }).MinimumLevel.Information();

    }

     public static void LogInformation(int LogId ,string msg, string functionname)
     {

            LogRecord record = new LogRecord
            {
                LogId = LogId,
                Message = msg,
                FunctionName = functionname,
                LogLevel = Level.INFORMATION,
                LogLevelId = Level.INFORMATION.GetHashCode()
            };

            Log.Information("{@holder}", record);

    }

from main iam calling

Logger log = new Logger(); Logger.EnableElasticSearch(); Logger.CreateLogger();

100 times Logger.LogInformation(123, "sadfdf", "asdad");

but only one value is coming.

if i use other function only first time log is coming Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogError(123, "sadfdf", "asdad"); Logger.LogWarning(123, "sadfdf", "asdad"); Logger.LogFatal(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad");

mivano commented 4 years ago

Did you dispose the logger? You need to close and flush the sinks at the end/closing of application. It has an internal buffer so it needs some time or an explicit flush to write them to ES.

Op 1 mrt. 2020 om 12:32 heeft Lucifer-Elf notifications@github.com het volgende geschreven:

 public Logger() { _loggerConfiguration = new LoggerConfiguration(); _loggerConfiguration.Enrich.WithMachineName().Enrich.WithProcessId().Enrich.WithThreadId() .MinimumLevel.Information();

}

public static void CreateLogger()
{
    Log.Logger =_loggerConfiguration.CreateLogger();

}

public static void EnableElasticSearch(string url = "")
{
    _loggerConfiguration.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/"))
      {
        AutoRegisterTemplate = true,
        AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
        IndexFormat = "serilog",
        CustomFormatter = new ExceptionAsObjectJsonFormatter(),

    }).MinimumLevel.Information();

}

 public static void LogInformation(int LogId ,string msg, string functionname)
 {

        LogRecord record = new LogRecord
        {
            LogId = LogId,
            Message = msg,
            FunctionName = functionname,
            LogLevel = Level.INFORMATION,
            LogLevelId = Level.INFORMATION.GetHashCode()
        };

        Log.Information("{@holder}", record);

}

from main iam calling

Logger log = new Logger(); Logger.EnableElasticSearch(); Logger.CreateLogger();

100 times Logger.LogInformation(123, "sadfdf", "asdad");

but only one value is coming.

if i use other function only first time log is coming Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogError(123, "sadfdf", "asdad"); Logger.LogWarning(123, "sadfdf", "asdad"); Logger.LogFatal(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad");

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Lucifer-Elf commented 4 years ago

okay i have added close and flush in close of this File but i have a simple task .

calling below function from main

Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogError(123, "sadfdf", "asdad"); Logger.LogWarning(123, "sadfdf", "asdad"); Logger.LogFatal(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad"); Logger.LogInformation(123, "sadfdf", "asdad");

and these should be in elastic search correct? but insead of 6 entry i always get 1st entry .

Lucifer-Elf commented 4 years ago

Logging.zip

please check with my test app and do let me know.

mivano commented 4 years ago

I advice you to look at the sample located here: https://github.com/serilog/serilog-sinks-elasticsearch/blob/dev/sample/Serilog.Sinks.Elasticsearch.Sample/Program.cs

You introduce a very complicated way of applying configurations and logging while there is so much simplicity already available out of the box. The destructor usage might also not be optimal.

To further troubleshoot; add a simple console sink and see if that does log. Keep in mind that the ES sink is a periodicbatching sink, so it buffers to max 50 items and 2 seconds before it flushes (and then sends it to ES). In a console app this means your app is closed before it has time to flush. See that explicit line here: https://github.com/serilog/serilog-sinks-elasticsearch/blob/dev/sample/Serilog.Sinks.Elasticsearch.Sample/Program.cs#L69

Lucifer-Elf commented 4 years ago

Hi Mivano, thanks I tried this one it works fine. But the project which I have created it works fin the console message come as the data but only elastic search data doesn't have all data. Is there any way to flush and not to close the connection . I have simple I should configur once Logger and use the static function to Log message in elastic search based on the log type.

Lucifer-Elf commented 4 years ago

is there is any way to flush all the data and not to close the connection? or after flush also to log data

mivano commented 4 years ago

There is already a static Log.Logger available, so use that one. For the buffer size, you can configure that to flush after each message, but that is not recommended.