-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Microsoft ILogger interface to push logs to skywalking (#…
…502) * feat : Add logging to skywalking * feat : Modify class library name * feat : 适配不通的.net 版本
- Loading branch information
Showing
14 changed files
with
288 additions
and
19 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
sample/SkyApm.Sample.Logging/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace SkyApm.Sample.Logging.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
_logger.LogInformation("查询时间"); | ||
|
||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
15 changes: 15 additions & 0 deletions
15
sample/SkyApm.Sample.Logging/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"profiles": { | ||
"SkyApm.Sample.Logging": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7126;http://localhost:5021", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Update="skyapm.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace SkyApm.Sample.Logging; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"SkyWalking": { | ||
"ServiceName": "asp-net-core-backend-logging", | ||
"Namespace": "", | ||
"HeaderVersions": [ | ||
"sw8" | ||
], | ||
"Sampling": { | ||
"SamplePer3Secs": -1, | ||
"Percentage": -1.0, | ||
"LogSqlParameterValue": false | ||
}, | ||
"Logging": { | ||
"Level": "Information", | ||
"FilePath": "logs/skyapm-{Date}.log" | ||
}, | ||
"Transport": { | ||
"Interval": 3000, | ||
"ProtocolVersion": "v8", | ||
"QueueSize": 30000, | ||
"BatchSize": 3000, | ||
"gRPC": { | ||
"Servers": "192.168.31.10:30102", | ||
"Timeout": 100000, | ||
"ConnectTimeout": 100000, | ||
"ReportTimeout": 600000 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/SkyApm.Diagnostics.MSLogging/SkyApm.Diagnostics.MSLogging.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks> | ||
<RootNamespace>SkyApm.Diagnostics.MSLogging</RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0"/> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'"> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\SkyApm.Abstractions\SkyApm.Abstractions.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
using SkyApm.Tracing; | ||
using SkyApm.Tracing.Segments; | ||
using SkyApm.Transport; | ||
|
||
namespace SkyApm.Diagnostics.MSLogging | ||
{ | ||
public class SkyApmLogger : ILogger | ||
{ | ||
private readonly string _categoryName; | ||
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher; | ||
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor; | ||
|
||
public SkyApmLogger(string categoryName, ISkyApmLogDispatcher skyApmLogDispatcher, IEntrySegmentContextAccessor entrySegmentContextAccessor) | ||
{ | ||
_categoryName = categoryName; | ||
_skyApmLogDispatcher = skyApmLogDispatcher; | ||
_entrySegmentContextAccessor = entrySegmentContextAccessor; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
var logs = new Dictionary<string, object>(); | ||
logs.Add("className", _categoryName); | ||
logs.Add("Level", logLevel); | ||
logs.Add("logMessage", state.ToString() ?? ""); | ||
var logContext = new LoggerContext() | ||
{ | ||
Logs = logs, | ||
SegmentContext = _entrySegmentContextAccessor.Context, | ||
}; | ||
_skyApmLogDispatcher.Dispatch(logContext); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
|
||
public IDisposable BeginScope<TState>(TState state) => default!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Concurrent; | ||
using Microsoft.Extensions.Logging; | ||
using SkyApm.Tracing; | ||
using SkyApm.Transport; | ||
|
||
namespace SkyApm.Diagnostics.MSLogging | ||
{ | ||
public class SkyApmLoggerProvider: ILoggerProvider | ||
{ | ||
private ConcurrentDictionary<string, SkyApmLogger> _doveLoggers = new ConcurrentDictionary<string, SkyApmLogger>(); | ||
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher; | ||
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor; | ||
|
||
public SkyApmLoggerProvider(ISkyApmLogDispatcher skyApmLogDispatcher,IEntrySegmentContextAccessor entrySegmentContextAccessor) | ||
{ | ||
_skyApmLogDispatcher = skyApmLogDispatcher; | ||
_entrySegmentContextAccessor = entrySegmentContextAccessor; | ||
|
||
} | ||
|
||
public void Dispose() | ||
{ | ||
|
||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return _doveLoggers.GetOrAdd(categoryName,_=>new SkyApmLogger(categoryName,_skyApmLogDispatcher,_entrySegmentContextAccessor)); | ||
} | ||
} | ||
} | ||
|