Skip to content

Commit

Permalink
feat: 添加平台标识,为不同平台代码可做预编译指令. (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Jul 9, 2024
2 parents c5f50fb + 955e14a commit 4e12b22
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
19 changes: 19 additions & 0 deletions sample/WebApi.Test.Unit/Common/SysHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if Windows
using System.Runtime.Versioning;
using System.Security.Principal;
#endif

namespace WebApi.Test.Unit.Common;

internal static class SysHelper
{
#if Windows
[SupportedOSPlatform("windows")]
internal static bool IsCurrentUserAdmin()
{
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
#endif
}
23 changes: 23 additions & 0 deletions sample/WebApi.Test.Unit/Controllers/MongoTestController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using EasilyNET.Core.Enums;
using EasilyNET.WebCore.Swagger.Attributes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System.ComponentModel;

Expand Down Expand Up @@ -94,6 +97,17 @@ public async Task<dynamic> Search(Search search)
var result = await db.Test2.Find(c => c.Date >= search.Start && c.Date <= search.End).ToListAsync();
return result;
}

/// <summary>
/// MultiEnum
/// </summary>
/// <returns></returns>
[HttpPost("MultiEnum")]
public async Task PostMultiEnum()
{
var coll = db.GetCollection<MultiEnum>(nameof(MultiEnum));
await coll.InsertOneAsync(new());
}
}

/// <summary>
Expand All @@ -119,4 +133,13 @@ public class Search
// ReSharper disable once UnusedMember.Global
[DefaultValue(30)]
public int Index { get; set; }
}

file sealed class MultiEnum
{
/// <summary>
/// IEnumerable类型的枚举需要添加该特性,才能实现每一个元素都转成字符串,可以参考: https://github.com/joesdu/EasilyNET/issues/482
/// </summary>
[BsonRepresentation(BsonType.String)]
public EZodiac[] Zodiac { get; set; } = [EZodiac., EZodiac., EZodiac.];
}
17 changes: 14 additions & 3 deletions sample/WebApi.Test.Unit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using Serilog.Events;
using Serilog.Sinks.OpenTelemetry;
using Serilog.Sinks.SystemConsole.Themes;
using System.Runtime.InteropServices;
using WebApi.Test.Unit;
using WebApi.Test.Unit.Common;
#if Windows
using System.Runtime.InteropServices;
#endif

Console.Title = $"❤️ {Constant.InstanceName}";
AssemblyHelper.AddExcludeLibs("Npgsql.");
Expand Down Expand Up @@ -41,11 +43,20 @@
//wt.SpectreConsole();
wt.Debug();
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#if Windows
Console.WriteLine("Is Windows");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && SysHelper.IsCurrentUserAdmin())
{
// 当为Windows系统时,添加事件日志
// 当为Windows系统时,添加事件日志,需要管理员权限才能写入Windows事件查看器
// 避免日志信息过多,仅将错误日志写入系统事件查看器
wt.EventLog(Constant.InstanceName, manageEventSource: true);
}
#endif
// Write To File
//wt.Map(le => (le.Timestamp.DateTime, le.Level), (key, log) =>
// log.Async(o => o.File($"logs{Path.DirectorySeparatorChar}{key.Level}{Path.DirectorySeparatorChar}.log",
// shared: true,
// rollingInterval: RollingInterval.Day)));
wt.Console(theme: AnsiConsoleTheme.Code);
wt.OpenTelemetry(c =>
{
Expand Down
15 changes: 15 additions & 0 deletions sample/WebApi.Test.Unit/WebApi.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>

<PropertyGroup Condition="'$(IsWindows)'=='true'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0-preview.5.24306.11" />
Expand All @@ -23,6 +36,8 @@
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2-dev-00338" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.EventLog" Version="4.0.1-dev-00087" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Map" Version="1.0.2" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.0.0-dev-00313" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
Expand Down

0 comments on commit 4e12b22

Please sign in to comment.