LemonStudio 1.0.3

dotnet add package LemonStudio --version 1.0.3
                    
NuGet\Install-Package LemonStudio -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LemonStudio" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LemonStudio" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="LemonStudio" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LemonStudio --version 1.0.3
                    
#r "nuget: LemonStudio, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LemonStudio@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LemonStudio&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=LemonStudio&version=1.0.3
                    
Install as a Cake Tool

LemonStudio

LemonStudio 是一个基于 ASP.NET Core 的快速开发框架,提供了完整的用户管理、权限控制、日志记录等功能。

快速开始

安装

dotnet new webapi -n Your.Project -controllers

cd Your.Project

dotnet add package LemonStudio

配置

Program.cs 中添加 LemonStudio 服务:

using FluentValidation;
using Lemon.Services.Extensions;

var builder = WebApplication.CreateBuilder(args);

// 配置 Serilog 日志(可选)
builder.UseLemonSerilog();

// 添加 LemonStudio 服务
builder.Services.AddLemonServices(builder.Configuration);

// 添加 OpenApi
builder.Services.AddOpenApi();

// 添加验证器
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

// 使用 LemonStudio 中间件
app.UseLemon(app.Environment.IsDevelopment());

app.MapControllers();

app.Run();

配置文件

创建 appsettings.json 配置文件:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Serilog": {
    "EnableFileLogging": true,
    "EnableConsoleLogging": true,
    "LogFilePath": "logs/app-.log",
    "RollingInterval": "Day",
    "RetainedFileCountLimit": 30,
    "FileSizeLimitBytes": 10485760,
    "MinimumLevel": "Information",
    "MinimumLevelOverrides": {
      "Microsoft": "Warning",
      "Microsoft.AspNetCore": "Warning",
      "System": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Redis": "localhost:6379"
  },
  "Databases": {
    "DefaultDatabase": "Default",
    "Connections": [
      {
        "Name": "Default",
        "Type": "PostgreSQL",
        "ConnectionString": "Host=localhost;Port=5432;Username=postgres;Password=your-password;Database=your-database;",
        "ConnectionPool": true,
        "AutoSyncStructure": true,
        "EnableMonitor": true
      },
      {
        "Name": "Secondary",
        "Type": "MySQL",
        "ConnectionString": "Server=localhost;User ID=root;Password=your-password;Database=secondary_db;",
        "ConnectionPool": true,
        "AutoSyncStructure": false,
        "EnableMonitor": false
      }
    ]
  },
  "Switch": {
    "Admin": true,
    "DataSeed": true
  },
  "Cors": {
    "AllowedOrigins": ["http://localhost:3000", "http://localhost:8080"]
  },
  "Jwt": [
    {
      "Name": "Admin",
      "SecretKey": "your-secret-key-here-must-be-at-least-32-characters",
      "Issuer": "Lemon.Api",
      "Audience": "Lemon.Client",
      "ExpiresInMinutes": 120,
      "SSO": false
    }
  ],
  "Response": {
    "CustomMessages": {
      "2001": "用户不存在",
      "2002": "密码错误",
      "2003": "用户已被禁用",
      "3001": "文件上传失败",
      "3002": "文件格式不支持",
      "3003": "文件大小超出限制",
      "4001": "业务规则验证失败",
      "4002": "数据同步失败",
      "4003": "第三方服务调用失败"
    }
  }
}

日志配置

LemonStudio 集成了 Serilog 日志框架,提供强大的日志记录功能。

启用 Serilog

Program.cs 中调用 UseLemonSerilog() 即可启用:

// 使用默认配置
builder.UseLemonSerilog();

// 或者自定义配置
builder.UseLemonSerilog(options =>
{
    options.LogFilePath = "/var/log/myapp/app-.log";
    options.MinimumLevel = "Debug";
    options.RetainedFileCountLimit = 60;
});
在代码中使用日志
public class MyController : ControllerBase
{
    private readonly ILogger<MyController> _logger;

    public MyController(ILogger<MyController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get()
    {
        _logger.LogInformation("处理 GET 请求");

        // 结构化日志
        _logger.LogInformation("用户 {UserId} 执行了 {Action} 操作", 123, "查询");

        return Ok();
    }
}
日志输出
  • 控制台: 开发环境实时查看
  • 文件: 生产环境持久化存储
  • Systemd Journal: 通过 journalctl 命令查看

详细配置说明请参考 Serilog 集成文档

多数据库使用方式

在业务服务中使用多数据库,可以继承 MultiDatabaseBaseService

using Lemon.Business.Base;
using Lemon.Services.Database;

public class MyService : MultiDatabaseBaseService
{
    public MyService(IFreeSql freeSql, IMultiDatabaseService multiDatabase)
        : base(freeSql, multiDatabase)
    {
    }

    public async Task<List<User>> GetUsersFromSecondaryDatabase()
    {
        // 获取指定数据库实例
        var secondaryDb = GetDatabase("Secondary");

        // 使用指定数据库进行操作
        return await secondaryDb.Select<User>().ToListAsync();
    }

    public async Task<List<User>> GetUsersFromDefaultDatabase()
    {
        // 使用默认数据库
        return await Db.Select<User>().ToListAsync();
    }
}

或者直接注入 IMultiDatabaseService

public class MyController : ControllerBase
{
    private readonly IMultiDatabaseService _multiDatabase;

    public MyController(IMultiDatabaseService multiDatabase)
    {
        _multiDatabase = multiDatabase;
    }

    [HttpGet]
    public async Task<IActionResult> GetData()
    {
        // 获取指定数据库
        var db = _multiDatabase.GetDatabase("Secondary");
        var data = await db.Select<MyEntity>().ToListAsync();

        return Success(data);
    }
}

许可证

本项目采用 MIT 许可证。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 136 7/9/2025
1.0.2 134 6/30/2025
1.0.1 133 6/23/2025
1.0.0 71 6/21/2025