Tutorial Serilog & Elastic Search

18. 12. 14
posted by: Admin
Hits: 56

Tutorial Serilog & Elastic Search

Serilog digunakan u/ menyimpan Log, misalnya jika terjadi error, maka akan terbentuk file log yg berisi keterangan penyebab error.

---Command prompt folder aplikasi WebApplication
dotnet add package Serilog
dotnet add package Serilog.Exceptions
dotnet add package Serilog.Sinks.Elasticsearch
dotnet add package Serilog.Extensions.logging
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.aspnetcore

---Di Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Builder;
//using Microsoft.AspNetCore.Hosting.IWebHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.AspNetCore;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.RollingFile;
using Serilog.Sinks.File;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Sinks.SystemConsole;
using Serilog.Extensions.Logging;

namespace WebApplication3
{
public class Program
{
public static int Main(string[] args)
{
//CreateWebHostBuilder(args).Build().Run();
var elasticsearchSinkOptions = new ElasticsearchSinkOptions(new Uri("http://localhost"))
{
MinimumLogEventLevel = LogEventLevel.Error,
IndexFormat = "latihan_asp3",
TypeName = "logException",
AutoRegisterTemplate = true,
BufferBaseFilename = @"C:\AspNetCoreLog\ElasticBuffer"
};

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.WriteTo.RollingFile(@"C:\AspNetCoreLog\LatihanASP3-{date}.log", LogEventLevel.Warning)
.WriteTo.Elasticsearch(elasticsearchSinkOptions)
.CreateLogger();

try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
.Build();
}
}


--Di HomeController.cs (u/ pengujian hasil Serilog) :
var ghi = 1;
var jkl = 3;
ghi = jkl / 0;

Add comment


Security code
Refresh