Skip to content

Commit

Permalink
Introduce Console formatter for Microsoft.Extensions.Logging.Console" (
Browse files Browse the repository at this point in the history
…#439)

- Introduce Console formatter Microsoft.Extensions.Logging
- Add README contents

---------

Co-authored-by: Steve Gordon <[email protected]>
  • Loading branch information
Mpdreamz and stevejgordon authored Sep 18, 2024
1 parent d429399 commit 9ec7e0c
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 217 deletions.
20 changes: 20 additions & 0 deletions ecs-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.NLog.Targets.Integr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "playground", "examples\playground\playground.csproj", "{86AEB76A-C210-4250-8541-B349C26C1683}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Extensions.Logging.Console", "src\Elastic.Extensions.Logging.Console\Elastic.Extensions.Logging.Console.csproj", "{E0033468-2448-47F5-8B7A-8DC1F5FF080C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Extensions.Logging.Console.Example", "examples\Elastic.Extensions.Logging.Console.Example\Elastic.Extensions.Logging.Console.Example.csproj", "{9656A08E-9DA6-473A-B3F8-245AC7B81A28}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Extensions.Logging.Common", "src\Elastic.Extensions.Logging.Common\Elastic.Extensions.Logging.Common.csproj", "{5EDF109F-9DFF-4957-8864-BA2702FB78F6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Serilog.Sinks.Tests", "tests\Elastic.Serilog.Sinks.Tests\Elastic.Serilog.Sinks.Tests.csproj", "{933FD923-A2DC-49E3-B21E-8BA888DB5924}"
EndProject
Global
Expand Down Expand Up @@ -283,6 +288,18 @@ Global
{86AEB76A-C210-4250-8541-B349C26C1683}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.Build.0 = Release|Any CPU
{E0033468-2448-47F5-8B7A-8DC1F5FF080C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0033468-2448-47F5-8B7A-8DC1F5FF080C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0033468-2448-47F5-8B7A-8DC1F5FF080C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0033468-2448-47F5-8B7A-8DC1F5FF080C}.Release|Any CPU.Build.0 = Release|Any CPU
{9656A08E-9DA6-473A-B3F8-245AC7B81A28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9656A08E-9DA6-473A-B3F8-245AC7B81A28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9656A08E-9DA6-473A-B3F8-245AC7B81A28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9656A08E-9DA6-473A-B3F8-245AC7B81A28}.Release|Any CPU.Build.0 = Release|Any CPU
{5EDF109F-9DFF-4957-8864-BA2702FB78F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EDF109F-9DFF-4957-8864-BA2702FB78F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EDF109F-9DFF-4957-8864-BA2702FB78F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EDF109F-9DFF-4957-8864-BA2702FB78F6}.Release|Any CPU.Build.0 = Release|Any CPU
{933FD923-A2DC-49E3-B21E-8BA888DB5924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{933FD923-A2DC-49E3-B21E-8BA888DB5924}.Debug|Any CPU.Build.0 = Debug|Any CPU
{933FD923-A2DC-49E3-B21E-8BA888DB5924}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -329,6 +346,9 @@ Global
{692F8035-F3F9-4714-8C9D-D54AF4CEB0E0} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{D1C3CAFB-A59D-4E3F-ADD1-4CB281E5349D} = {947B298F-9139-4868-B337-729541932E4D}
{86AEB76A-C210-4250-8541-B349C26C1683} = {05075402-8669-45BD-913A-BD40A29BBEAB}
{E0033468-2448-47F5-8B7A-8DC1F5FF080C} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{9656A08E-9DA6-473A-B3F8-245AC7B81A28} = {05075402-8669-45BD-913A-BD40A29BBEAB}
{5EDF109F-9DFF-4957-8864-BA2702FB78F6} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
{933FD923-A2DC-49E3-B21E-8BA888DB5924} = {3582B07D-C2B0-49CC-B676-EAF806EB010E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
5 changes: 3 additions & 2 deletions ecs-dotnet.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
&lt;/Entry.Match&gt;&#xD;
&lt;Entry.SortBy&gt;&#xD;
&lt;Kind Is="Member" /&gt;&#xD;
&lt;Name Is="Enter Pattern Here" /&gt;&#xD;
&lt;Name /&gt;&#xD;
&lt;/Entry.SortBy&gt;&#xD;
&lt;/Entry&gt;&#xD;
&lt;Entry DisplayName="Readonly Fields"&gt;&#xD;
Expand All @@ -137,7 +137,7 @@
&lt;Entry.SortBy&gt;&#xD;
&lt;Access /&gt;&#xD;
&lt;Readonly /&gt;&#xD;
&lt;Name Is="Enter Pattern Here" /&gt;&#xD;
&lt;Name /&gt;&#xD;
&lt;/Entry.SortBy&gt;&#xD;
&lt;/Entry&gt;&#xD;
&lt;Entry DisplayName="Constructors"&gt;&#xD;
Expand Down Expand Up @@ -411,6 +411,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002EMemberReordering_002EMigrations_002ECSharpFileLayoutPatternRemoveIsAttributeUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/HighlightingManager/HighlightingEnabledByDefault/@EntryValue">False</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Elastic.Extensions.Logging.Console\Elastic.Extensions.Logging.Console.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Elastic.Extensions.Logging.Console.Example;

/// <summary> Simulate work that logs in low volume with some time in between each log call </summary>
public class ExampleService : BackgroundService
{
private readonly ILogger<ExampleService> _logger;

public ExampleService(ILogger<ExampleService> logger) => _logger = logger;

protected override async Task ExecuteAsync(CancellationToken ctx)
{
for (var i = 0; i < 100; i++)
{
if (i % 10 == 0)
_logger.LogWarning("We are logging way too much: {CustomData}", i);
else
_logger.LogInformation("We are logging way too much: {CustomData}", i);
if (i % 100 == 0)
await Task.Delay(1, ctx);
}
}
}
23 changes: 23 additions & 0 deletions examples/Elastic.Extensions.Logging.Console.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// See https://aka.ms/new-console-template for more information

using Elastic.Extensions.Logging.Console;
using Elastic.Extensions.Logging.Console.Example;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

await Host.CreateDefaultBuilder(args)
.UseConsoleLifetime()
.ConfigureAppConfiguration((_, configurationBuilder) =>
{
configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
})
.ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddEcsConsole())
.ConfigureServices((_, services) =>
{
services.AddHostedService<ExampleService>();
})
.Build()
.RunAsync();

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Elastic.Elasticsearch.Ephemeral" Version="0.4.3" />
<PackageReference Include="NEST" Version="7.8.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Title>Common Abstactions For ECS For Microsoft.Extensions.Logging</Title>
<Description>Transient dependency, do not install directly. Common Abstactions For ECS For Microsoft.Extensions.Logging</Description>
<PackageTags>Logging;LoggerProvider;Elasticsearch;Console;ELK;Kibana;Logstash;Tracing;Diagnostics;Log;Trace;ECS</PackageTags>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>True</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Elastic.CommonSchema\Elastic.CommonSchema.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0"/>
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Elastic.CommonSchema;

namespace Elastic.Extensions.Logging.Common;

/// <summary>
///
/// </summary>
public interface ILogEventCreationOptions : IEcsDocumentCreationOptions
{
/// <summary>
/// Gets or sets additional tags to pass in the message, for example you can tag with the environment name ('Development',
/// 'Production', etc).
/// </summary>
string[]? Tags { get; set; }

/// <summary>
/// Gets or sets the separate to use for <c>IList</c> semantic values.
/// </summary>
string ListSeparator { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Elastic.CommonSchema;
using Elastic.CommonSchema.Serialization;

// kept in this namespace for bwc
// ReSharper disable once CheckNamespace
namespace Elastic.Extensions.Logging
{
/// <summary>
Expand Down
Loading

0 comments on commit 9ec7e0c

Please sign in to comment.