serilog-sinks-elasticsearch icon indicating copy to clipboard operation
serilog-sinks-elasticsearch copied to clipboard

Logger send elastic one value at a time

Open Lucifer-Elf opened this issue 4 years ago • 8 comments

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?

  • [ ] Bug
  • [ ] New Feature

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.

  • [ ] netCore 2.0
  • [ ] netCore 1.0
  • [ ] 4.7
  • [ ] 4.6.x
  • [ ] 4.5.x

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 avatar Mar 01 '20 11:03 Lucifer-Elf

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");

Lucifer-Elf avatar Mar 01 '20 11:03 Lucifer-Elf

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 [email protected] 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.

mivano avatar Mar 01 '20 11:03 mivano

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 avatar Mar 01 '20 11:03 Lucifer-Elf

Logging.zip

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

Lucifer-Elf avatar Mar 01 '20 12:03 Lucifer-Elf

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

mivano avatar Mar 01 '20 20:03 mivano

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 avatar Mar 02 '20 02:03 Lucifer-Elf

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

Lucifer-Elf avatar Mar 02 '20 04:03 Lucifer-Elf

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.

mivano avatar Mar 03 '20 16:03 mivano