練習資料庫紀錄完成
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coreprofiler" Version="1.1.4" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
14
CoreProfilerExample.Common/IDatabaseOption.cs
Normal file
14
CoreProfilerExample.Common/IDatabaseOption.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Common
|
||||
{
|
||||
public interface IDatabaseOption
|
||||
{
|
||||
public IDbConnection SqliteConnection { get; }
|
||||
}
|
||||
}
|
||||
30
CoreProfilerExample.Common/Options/DatabaseOption.cs
Normal file
30
CoreProfilerExample.Common/Options/DatabaseOption.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using CoreProfiler;
|
||||
using CoreProfiler.Data;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Common.Options
|
||||
{
|
||||
public class DatabaseOption(IConfiguration configuration) : IDatabaseOption
|
||||
{
|
||||
public IDbConnection SqliteConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString("SQLITE");
|
||||
|
||||
var dbProfiler = ProfilingSession.Current != null
|
||||
? new DbProfiler(ProfilingSession.Current.Profiler)
|
||||
: null;
|
||||
|
||||
return new ProfiledDbConnection(new SqliteConnection(connectionString), dbProfiler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,23 +1,47 @@
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfiler;
|
||||
using CoreProfilerExample.Common;
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using Dapper;
|
||||
using System.Reflection;
|
||||
using static CoreProfilerExample.Common.Constants.WeatherConstant;
|
||||
|
||||
namespace CoreProfilerExample.Repository.Implements
|
||||
{
|
||||
public class WeatherForecastRepository : IWeatherForecastRepository
|
||||
public class WeatherForecastRepository(IDatabaseOption option) : IWeatherForecastRepository
|
||||
{
|
||||
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
public async Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
{
|
||||
var type = MethodBase.GetCurrentMethod()?.DeclaringType;
|
||||
|
||||
var name = MethodBase.GetCurrentMethod()?.Name;
|
||||
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
var forecasts = days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)).ToString(),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)]
|
||||
}) : []);
|
||||
}) : [];
|
||||
|
||||
var sql = @"INSERT INTO [WeatherForecast] (
|
||||
[Date],
|
||||
[TemperatureC],
|
||||
[Summary]
|
||||
)
|
||||
VALUES (
|
||||
@Date,
|
||||
@TemperatureC,
|
||||
@Summary
|
||||
)";
|
||||
|
||||
using var connection = option.SqliteConnection;
|
||||
|
||||
await connection.ExecuteAsync(sql, forecasts);
|
||||
|
||||
return forecasts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public class WeatherForecastDataModel
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public string Date { get; set; } = null!;
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public class GetWeatherForecastItemDto
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public string Date { get; set; } = null!;
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CoreProfiler.Web" Version="1.1.4" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
BIN
CoreProfilerExample/CoreProfilerExample.db
Normal file
BIN
CoreProfilerExample/CoreProfilerExample.db
Normal file
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
using CoreProfiler.Web;
|
||||
using CoreProfilerExample.Common;
|
||||
using CoreProfilerExample.Common.Options;
|
||||
using CoreProfilerExample.Repository.Implements;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
@@ -9,6 +10,12 @@ namespace CoreProfilerExample.Infrastructure.Extensions
|
||||
{
|
||||
public static class DependencyInjectionExtension
|
||||
{
|
||||
public static IServiceCollection AddOption(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IDatabaseOption, DatabaseOption>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddRepository(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public class GetWeatherForecastItemViewModel
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public string Date { get; set; } = null!;
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
#region CoreProfilerExample
|
||||
builder.Services.AddOption();
|
||||
builder.Services.AddRepository();
|
||||
builder.Services.AddService();
|
||||
#endregion
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
"ConnectionStrings": {
|
||||
"SQLITE": "Data Source=CoreProfilerExample.db"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
3
CoreProfilerExample/appsettings.Production.json
Normal file
3
CoreProfilerExample/appsettings.Production.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -1,9 +1,3 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
20
README.md
20
README.md
@@ -117,3 +117,23 @@ public static IApplicationBuilder UseCoreProfiler(this IApplicationBuilder app)
|
||||
紀錄的詳細如下:
|
||||
|
||||

|
||||
|
||||
## CoreProfiler extends DB
|
||||
|
||||
CoreProfiler也可用於紀錄SQL執行,將原本資料庫使用的<code>Connection</code>替換成CoreProfiler實作的<code>Connection</code>就可以紀錄執行的T-SQL指令。
|
||||
|
||||
替換方法如下:
|
||||
|
||||
```C#
|
||||
var connectionString = configuration.GetConnectionString("SQLITE");
|
||||
|
||||
var dbProfiler = ProfilingSession.Current != null
|
||||
? new DbProfiler(ProfilingSession.Current.Profiler)
|
||||
: null;
|
||||
|
||||
return new ProfiledDbConnection(new SqliteConnection(connectionString), dbProfiler);
|
||||
```
|
||||
|
||||
在替換之前,先確認CoreProfiler是否有運行的實體,才替換資料庫連線,執行結果如下:
|
||||
|
||||

|
||||
Reference in New Issue
Block a user