Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public IndexModel(ILogger<IndexModel> logger)

public void OnGet()
{

var myName = "OK";
_logger.LogInformation("Sample log. My name is {MyName}", myName);
}
}
72 changes: 57 additions & 15 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,67 @@
using Elastic.Channels;
using Elastic.Ingest.Elasticsearch;
using Elastic.Ingest.Elasticsearch.DataStreams;
using Elastic.Serilog.Sinks;
using Elastic.Transport;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/start-host-log-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
try
{
builder.Services.AddRazorPages();

builder.Host.UseSerilog();

var app = builder.Build();
builder.Services.AddSerilog((_, lc) => lc.Enrich.FromLogContext()
.WriteTo.Elasticsearch([new Uri("http://localhost:9200")], opts =>
{
opts.DataStream = new DataStreamName("logs", "telemetry-loggin", "demo");
opts.BootstrapMethod = BootstrapMethod.Failure;
opts.ConfigureChannel = channelOpts =>
{
channelOpts.BufferOptions = new BufferOptions
{
ExportMaxConcurrency = 10
};
};
}, transport =>
{
transport.Authentication(new BasicAuthentication("elastic", "changeme")); // Basic Auth
// transport.Authentication(new ApiKey(base64EncodedApiKey)); // ApiKey
transport.OnRequestCompleted(d => Console.WriteLine($"es-req: {d.DebugInformation}"));
})
.Enrich.WithProperty("Environment", builder.Environment.EnvironmentName)
.ReadFrom.Configuration(builder.Configuration));
//builder.Services.AddSerilog(Log.Logger);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseSerilogRequestLogging();
app.UseStaticFiles();

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.MapRazorPages();
app.MapRazorPages();

app.Run();
app.Run();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
9 changes: 0 additions & 9 deletions appsettings.Development.json

This file was deleted.

28 changes: 24 additions & 4 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
{
"Logging": {
"LogLevel": {
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": ".logs/log-.txt",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
}
}
],
"MinimumLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
}
},
"AllowedHosts": "*"
}
}
7 changes: 7 additions & 0 deletions telemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elastic.Serilog.Sinks" Version="9.0.0" />
<PackageReference Include="Serilog" Version="4.3.1-dev-02390" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
</ItemGroup>

</Project>