Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeSql Diagnostics #461

Merged
merged 3 commits into from
Jan 25, 2022
Merged
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
52 changes: 26 additions & 26 deletions sample/SkyApm.Sample.Backend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using Microsoft.AspNetCore;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;

namespace SkyApm.Sample.Backend
{
public class Program
{
public static void Main(string[] args)
{
BuildHost(args).Run();
using Microsoft.AspNetCore;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
namespace SkyApm.Sample.Backend
{
public class Program
{
public static void Main(string[] args)
{
BuildHost(args).Run();
}

#if NETCOREAPP2_1
#if NETCOREAPP2_1

public static IWebHost BuildHost(string[] args) =>
WebHost.CreateDefaultBuilder<Startup>(args)
.Build();

#else

public static IWebHost BuildHost(string[] args) =>
WebHost.CreateDefaultBuilder<Startup>(args)
.Build();
public static IHost BuildHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
}).Build();

#else

public static IHost BuildHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
}).Build();

#endif
}
#endif
}
}
82 changes: 82 additions & 0 deletions sample/SkyApm.Sample.FreeSql/Controllers/SongController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Microsoft.AspNetCore.Mvc;
using SkyApm.Sample.FreeSqlSqlite.Entitys;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SkyApm.Sample.FreeSqlSqlite.Controllers
{


[Route("api/[controller]")]
public class SongsController : Controller
{

IFreeSql _fsql;

public SongsController(IFreeSql fsql)
{
_fsql = fsql;
}

[HttpGet]
public Task<List<Song>> GetItems([FromQuery] string key, [FromQuery] int page = 1, [FromQuery] int limit = 20)
{
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync();
}

/// <summary>
/// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain"
/// </summary>
/// <param name="key"></param>
/// <param name="pagingInfo"></param>
/// <returns></returns>
[HttpGet("GetPagingItems")]
public Task<List<Song>> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo)
{
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync();
}

[HttpGet("{id}")]
public async Task<Song> GetItem([FromRoute] int id)
{
var s= await _fsql.Select<Song>().Where(a => a.Id == id).ToOneAsync();
return s;
}

public class ModelSong
{
public string title { get; set; }
}

[HttpPost, ProducesResponseType(201)]
async public Task<Song> Create([FromBody] ModelSong model)
{
var ret = await _fsql.Insert<Song>().AppendData(new Song { Title = model.title }).ExecuteInsertedAsync();
return ret.FirstOrDefault();
}

[HttpPut("{id}")]
async public Task<Song> Update([FromRoute] int id, [FromBody] ModelSong model)
{
var ret = await _fsql.Update<Song>().SetSource(new Song { Id = id, Title = model.title }).ExecuteUpdatedAsync();
return ret.FirstOrDefault();
}

[HttpPatch("{id}")]
async public Task<Song> UpdateDiy([FromRoute] int id, [FromForm] string title)
{
var up = _fsql.Update<Song>().Where(a => a.Id == id);
if (!string.IsNullOrEmpty(title)) up.Set(a => a.Title, title);
var ret = await up.ExecuteUpdatedAsync();
return ret.FirstOrDefault();
}

[HttpDelete("{id}"), ProducesResponseType(204)]
async public Task<Song> Delete([FromRoute] int id)
{
var ret = await _fsql.Delete<Song>().Where(a => a.Id == id).ExecuteDeletedAsync();
return ret.FirstOrDefault();
}
}
}
12 changes: 12 additions & 0 deletions sample/SkyApm.Sample.FreeSql/Entitys/Song.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FreeSql.DataAnnotations;

namespace SkyApm.Sample.FreeSqlSqlite.Entitys
{
public class Song
{

[Column(IsIdentity = true)]
public int Id { get; set; }
public string Title { get; set; }
}
}
57 changes: 57 additions & 0 deletions sample/SkyApm.Sample.FreeSql/PagingInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SkyApm.Sample.FreeSqlSqlite
{
public class PagingInfo : BasePagingInfo
{
/// <summary>
/// 无参构造函数
/// </summary>
public PagingInfo()
{
}
/// <summary>
/// 当前为第1页,每页大小的构造函数
/// </summary>
/// <param name="pageSize"></param>
public PagingInfo(int pageSize)
{
PageNumber = 1;
PageSize = pageSize;
}
/// <summary>
/// 带当前页和每页大小的构造函数
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
public PagingInfo(int pageNumber, int pageSize)
{
PageNumber = pageNumber;
PageSize = pageSize;
}
/// <summary>
/// 当前有多少页【只读】
/// </summary>
public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize;
/// <summary>
/// 是否有上一页【只读】
/// </summary>
public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount;
/// <summary>
/// 是否有下一页【只读】
/// </summary>
public bool HasNext => PageNumber < PageCount;
/// <summary>
/// 是否在第一页【只读】
/// </summary>
public bool IsFrist => PageNumber == 1;
/// <summary>
/// 是否在最后一页【只读】
/// </summary>
public bool IsLast => PageNumber == PageCount;
}
}
22 changes: 22 additions & 0 deletions sample/SkyApm.Sample.FreeSql/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;

namespace SkyApm.Sample.FreeSqlSqlite
{
public class Program
{
public static void Main(string[] args)
{
BuildHost(args).Run();
}

public static IHost BuildHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
}).Build();

}
}
27 changes: 27 additions & 0 deletions sample/SkyApm.Sample.FreeSql/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SkyApm.Sample.FreeSqlSqlite": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000/"
}
}
}
29 changes: 29 additions & 0 deletions sample/SkyApm.Sample.FreeSql/SkyApm.Sample.FreeSqlSqlite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
<PackageReference Include="FreeSql" Version="3.0.100" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.0.100" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\SkyApm.Diagnostics.FreeSql\SkyApm.Diagnostics.FreeSql.csproj" />

</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Development.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="skyapm.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
78 changes: 78 additions & 0 deletions sample/SkyApm.Sample.FreeSql/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using FreeSql;
using SkyApm.Diagnostics.FreeSql;
using SkyApm.Sample.FreeSqlSqlite.Entitys;
using System;
using System.Collections.Generic;
using SkyApm.Utilities.DependencyInjection;

namespace SkyApm.Sample.FreeSqlSqlite
{
public class Startup
{
private readonly string[] Titles = new string[] { "GetAway", "Candy", "DreamHeaven"};
public Startup(IConfiguration configuration)
{
Configuration = configuration;

Fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"DataSource=:memory:;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.Build();
InitData();
Fsql.Aop.CurdAfter += (s, e) =>
{
if (e.ElapsedMilliseconds > 200)
{
//记录日志
//发送短信给负责人
}
};


}

public IConfiguration Configuration { get; }
public IFreeSql Fsql { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IFreeSql>(Fsql);
services.AddControllers();
services.AddSkyApmExtensions();
services.AddSkyAPM((e) =>
{
e.AddFreeSql(Fsql);
});
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{

app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(a => a.MapControllers());
}


public void InitData()
{
const int count= 10000;
var rand = new Random();
var songs = new List<Song>(count);
for (int i = 0; i < count; i++)
{
songs.Add(new Song() { Id = i + 1, Title = Titles[rand.Next(Titles.Length - 1)] });

}
Fsql.Insert<Song>(songs).ExecuteIdentity();


}
}

}
10 changes: 10 additions & 0 deletions sample/SkyApm.Sample.FreeSql/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
8 changes: 8 additions & 0 deletions sample/SkyApm.Sample.FreeSql/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Loading