diff --git a/CoreProfilerExample.Common/CoreProfilerExample.Common.csproj b/CoreProfilerExample.Common/CoreProfilerExample.Common.csproj index 04296cf..be22c5b 100644 --- a/CoreProfilerExample.Common/CoreProfilerExample.Common.csproj +++ b/CoreProfilerExample.Common/CoreProfilerExample.Common.csproj @@ -8,6 +8,7 @@ + diff --git a/CoreProfilerExample.Common/IDatabaseOption.cs b/CoreProfilerExample.Common/IDatabaseOption.cs new file mode 100644 index 0000000..6aa9ce2 --- /dev/null +++ b/CoreProfilerExample.Common/IDatabaseOption.cs @@ -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; } + } +} diff --git a/CoreProfilerExample.Common/Options/DatabaseOption.cs b/CoreProfilerExample.Common/Options/DatabaseOption.cs new file mode 100644 index 0000000..5f2d7eb --- /dev/null +++ b/CoreProfilerExample.Common/Options/DatabaseOption.cs @@ -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); + } + } + } +} diff --git a/CoreProfilerExample.Repository/CoreProfilerExample.Repository.csproj b/CoreProfilerExample.Repository/CoreProfilerExample.Repository.csproj index e4acc0c..e79e187 100644 --- a/CoreProfilerExample.Repository/CoreProfilerExample.Repository.csproj +++ b/CoreProfilerExample.Repository/CoreProfilerExample.Repository.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/CoreProfilerExample.Repository/Implements/WeatherForecastRepository.cs b/CoreProfilerExample.Repository/Implements/WeatherForecastRepository.cs index 494fb17..3636ba9 100644 --- a/CoreProfilerExample.Repository/Implements/WeatherForecastRepository.cs +++ b/CoreProfilerExample.Repository/Implements/WeatherForecastRepository.cs @@ -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> GetAsync(int days) + public async Task> 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; } } } diff --git a/CoreProfilerExample.Repository/Models/DataModels/WeatherForecastDataModel.cs b/CoreProfilerExample.Repository/Models/DataModels/WeatherForecastDataModel.cs index c8ee910..9febc42 100644 --- a/CoreProfilerExample.Repository/Models/DataModels/WeatherForecastDataModel.cs +++ b/CoreProfilerExample.Repository/Models/DataModels/WeatherForecastDataModel.cs @@ -2,7 +2,7 @@ { public class WeatherForecastDataModel { - public DateOnly Date { get; set; } + public string Date { get; set; } = null!; public int TemperatureC { get; set; } diff --git a/CoreProfilerExample.Service/Models/Dtos/GetWeatherForecastItemDto.cs b/CoreProfilerExample.Service/Models/Dtos/GetWeatherForecastItemDto.cs index 0565875..4f3d5da 100644 --- a/CoreProfilerExample.Service/Models/Dtos/GetWeatherForecastItemDto.cs +++ b/CoreProfilerExample.Service/Models/Dtos/GetWeatherForecastItemDto.cs @@ -2,7 +2,7 @@ { public class GetWeatherForecastItemDto { - public DateOnly Date { get; set; } + public string Date { get; set; } = null!; public int TemperatureC { get; set; } diff --git a/CoreProfilerExample/CoreProfilerExample.csproj b/CoreProfilerExample/CoreProfilerExample.csproj index 7118ec8..f5a580e 100644 --- a/CoreProfilerExample/CoreProfilerExample.csproj +++ b/CoreProfilerExample/CoreProfilerExample.csproj @@ -8,6 +8,7 @@ + diff --git a/CoreProfilerExample/CoreProfilerExample.db b/CoreProfilerExample/CoreProfilerExample.db new file mode 100644 index 0000000..f44befe Binary files /dev/null and b/CoreProfilerExample/CoreProfilerExample.db differ diff --git a/CoreProfilerExample/Infrastructure/Extensions/DependencyInjectionExtension.cs b/CoreProfilerExample/Infrastructure/Extensions/DependencyInjectionExtension.cs index b8eda56..baf4980 100644 --- a/CoreProfilerExample/Infrastructure/Extensions/DependencyInjectionExtension.cs +++ b/CoreProfilerExample/Infrastructure/Extensions/DependencyInjectionExtension.cs @@ -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(); + return services; + } + public static IServiceCollection AddRepository(this IServiceCollection services) { services.AddScoped(); diff --git a/CoreProfilerExample/Models/ViewModels/GetWeatherForecastItemViewModel.cs b/CoreProfilerExample/Models/ViewModels/GetWeatherForecastItemViewModel.cs index fae57c1..a50c68b 100644 --- a/CoreProfilerExample/Models/ViewModels/GetWeatherForecastItemViewModel.cs +++ b/CoreProfilerExample/Models/ViewModels/GetWeatherForecastItemViewModel.cs @@ -2,7 +2,7 @@ { public class GetWeatherForecastItemViewModel { - public DateOnly Date { get; set; } + public string Date { get; set; } = null!; public int TemperatureC { get; set; } diff --git a/CoreProfilerExample/Program.cs b/CoreProfilerExample/Program.cs index f7e249a..7d98ef2 100644 --- a/CoreProfilerExample/Program.cs +++ b/CoreProfilerExample/Program.cs @@ -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 diff --git a/CoreProfilerExample/appsettings.Development.json b/CoreProfilerExample/appsettings.Development.json index 0c208ae..599eabe 100644 --- a/CoreProfilerExample/appsettings.Development.json +++ b/CoreProfilerExample/appsettings.Development.json @@ -1,8 +1,6 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } + "ConnectionStrings": { + "SQLITE": "Data Source=CoreProfilerExample.db" + }, + "AllowedHosts": "*" } diff --git a/CoreProfilerExample/appsettings.Production.json b/CoreProfilerExample/appsettings.Production.json new file mode 100644 index 0000000..6b85907 --- /dev/null +++ b/CoreProfilerExample/appsettings.Production.json @@ -0,0 +1,3 @@ +{ + "AllowedHosts": "*" +} \ No newline at end of file diff --git a/CoreProfilerExample/appsettings.json b/CoreProfilerExample/appsettings.json index 10f68b8..4faf77a 100644 --- a/CoreProfilerExample/appsettings.json +++ b/CoreProfilerExample/appsettings.json @@ -1,9 +1,3 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, "AllowedHosts": "*" } diff --git a/CoreProfilerExampleResource/LastestProfilingResultDetailForDB.JPG b/CoreProfilerExampleResource/LastestProfilingResultDetailForDB.JPG new file mode 100644 index 0000000..41ae0a7 Binary files /dev/null and b/CoreProfilerExampleResource/LastestProfilingResultDetailForDB.JPG differ diff --git a/README.md b/README.md index 7de1725..ff08429 100644 --- a/README.md +++ b/README.md @@ -116,4 +116,24 @@ public static IApplicationBuilder UseCoreProfiler(this IApplicationBuilder app) ¬ö¿ýªº¸Ô²Ó¦p¤U: -![LastestProfilingResultDetail](CoreProfilerExampleResource/LastestProfilingResultDetail.JPG) \ No newline at end of file +![LastestProfilingResultDetail](CoreProfilerExampleResource/LastestProfilingResultDetail.JPG) + +## CoreProfiler extends DB + +CoreProfiler¤]¥i¥Î©ó¬ö¿ýSQL°õ¦æ¡A±N­ì¥»¸ê®Æ®w¨Ï¥ÎªºConnection´À´«¦¨CoreProfiler¹ê§@ªºConnection´N¥i¥H¬ö¿ý°õ¦æªºT-SQL«ü¥O¡C + +´À´«¤èªk¦p¤U: + +```C# +var connectionString = configuration.GetConnectionString("SQLITE"); + +var dbProfiler = ProfilingSession.Current != null + ? new DbProfiler(ProfilingSession.Current.Profiler) + : null; + +return new ProfiledDbConnection(new SqliteConnection(connectionString), dbProfiler); +``` + +¦b´À´«¤§«e¡A¥ý½T»{CoreProfiler¬O§_¦³¹B¦æªº¹êÅé¡A¤~´À´«¸ê®Æ®w³s½u¡A°õ¦æµ²ªG¦p¤U: + +![LastestProfilingResultDetailForDB](CoreProfilerExampleResource/LastestProfilingResultDetailForDB.JPG) \ No newline at end of file