Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0091af6ae | ||
|
|
70df39c4c8 | ||
|
|
d44f364924 | ||
|
|
4095c3c4a8 | ||
|
|
1f94f028c6 | ||
|
|
d34ba4e43a | ||
|
|
c02f05bcdf | ||
|
|
3e4cf7c554 |
|
|
@ -1,4 +1,13 @@
|
|||
using System.Diagnostics;
|
||||
using CodeRelease.Model;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using Serilog;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace CodeRelease.BLL
|
||||
{
|
||||
|
|
@ -19,6 +28,7 @@ namespace CodeRelease.BLL
|
|||
int exitCode = 0;
|
||||
try
|
||||
{
|
||||
Log.Logger.Information($"开始执行linux {command}");
|
||||
// 创建一个新的进程
|
||||
using (Process process = new Process())
|
||||
{
|
||||
|
|
@ -32,19 +42,24 @@ namespace CodeRelease.BLL
|
|||
// 绑定输出和错误数据接收事件
|
||||
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
|
||||
{
|
||||
//Log.Logger.Information($"linux执行出错OutputDataReceived,命令{command}");
|
||||
//Log.Logger.Information($"linux执行出错OutputDataReceived,Output{JsonConvert.SerializeObject(e)}");
|
||||
if (!string.IsNullOrEmpty(e.Data))
|
||||
{
|
||||
Console.WriteLine($"Output: {e.Data}");
|
||||
outputDataReceived = e.Data;
|
||||
//Console.WriteLine($"Output: {e.Data}");
|
||||
|
||||
outputDataReceived += e.Data + "\n";
|
||||
}
|
||||
});
|
||||
|
||||
process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
|
||||
{
|
||||
// Log.Logger.Information($"linux执行出错ErrorDataReceived,命令{command}");
|
||||
//Log.Logger.Information($"linux执行出错ErrorDataReceived,Output:{JsonConvert.SerializeObject(e)}");
|
||||
if (!string.IsNullOrEmpty(e.Data))
|
||||
{
|
||||
Console.WriteLine($"Error: {e.Data}");
|
||||
errorDataReceived = e.Data;
|
||||
//Console.WriteLine($"Error: {e.Data}");
|
||||
errorDataReceived += e.Data + "\n";
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -59,13 +74,16 @@ namespace CodeRelease.BLL
|
|||
await process.WaitForExitAsync();
|
||||
|
||||
// 输出退出代码
|
||||
Console.WriteLine($"Process exited with code {process.ExitCode}");
|
||||
//Console.WriteLine($"Process exited with code {process.ExitCode}");
|
||||
Log.Logger.Information($"执行结束:{process.ExitCode}");
|
||||
Log.Logger.Information($"{outputDataReceived}");
|
||||
Log.Logger.Information($"{errorDataReceived}");
|
||||
exitCode = process.ExitCode;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Exception: {ex.Message}");
|
||||
Log.Logger.Information($"执行出现异常结束 {ex.Message}");
|
||||
}
|
||||
return new { exitCode, outputDataReceived, errorDataReceived };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,12 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Serilog" Version="4.0.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.Text.Json" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ using Newtonsoft.Json;
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
using Serilog;
|
||||
|
||||
using System;
|
||||
|
||||
|
||||
|
|
@ -17,7 +19,7 @@ namespace CodeRelease.Controllers
|
|||
|
||||
[Route("api/[controller]/[Action]")]
|
||||
[ApiController]
|
||||
public class PublishController : ControllerBase
|
||||
public class PublishController() : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[HttpPost]
|
||||
|
|
@ -42,6 +44,7 @@ namespace CodeRelease.Controllers
|
|||
/// <returns></returns>
|
||||
public async Task<object> ReleaseAction([FromBody] object data, [FromServices] GiteaWebhookConfigModel giteaWebhookConfig)
|
||||
{
|
||||
Log.Logger.Information("执行发布接口,参数{data}", data);
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings
|
||||
{
|
||||
//MaxDepth = 64 // 设置一个足够大的值,默认值是 32
|
||||
|
|
@ -85,18 +88,16 @@ namespace CodeRelease.Controllers
|
|||
else if (!string.IsNullOrEmpty(releaseAction.Release.TargetCommitish))
|
||||
{
|
||||
var version = releaseAction.Release.TagName;
|
||||
|
||||
var download = giteaWebhookConfig.BrowserDownloadFileUrl + version + "/";
|
||||
if (!Directory.Exists(download))
|
||||
{
|
||||
Directory.CreateDirectory(download);
|
||||
}
|
||||
FileDownloader fileDownloader = new FileDownloader();
|
||||
//giteaWebhookConfig.BrowserDownloadFileUrl.Replace("{version}", version);
|
||||
var mingling = giteaWebhookConfig.MakeFile
|
||||
.Replace("{version}", version)
|
||||
.Replace("{dir_file_path}", download + "/")
|
||||
.Replace("{description}", releaseAction.Release.Body)
|
||||
.Replace("{name}", version)
|
||||
//.Replace("{version}", version)
|
||||
//.Replace("{version}", version)
|
||||
//.Replace("{version}", version)
|
||||
//.Replace("{dir_file_path}", download + "/")
|
||||
.Replace("{target_commitish}", releaseAction.Release.TargetCommitish);
|
||||
Log.Logger.Information("执行linux命令{mingling}", mingling);
|
||||
LinuxExecuteCommand linuxExecuteCommand = new LinuxExecuteCommand();
|
||||
//make public version=0.0.1 dir_file_path=/
|
||||
var obj = await linuxExecuteCommand.ExecuteCommand(mingling);
|
||||
|
|
|
|||
|
|
@ -3,9 +3,34 @@ using CodeRelease.Utile;
|
|||
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
|
||||
using Serilog;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog.Events;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
//.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
|
||||
.Build();
|
||||
|
||||
var logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.CreateLogger();
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
//.SetBasePath(Directory.GetCurrentDirectory())
|
||||
//.AddJsonFile("appsettings.json")
|
||||
.WriteTo.Console()
|
||||
.WriteTo.Debug()
|
||||
.WriteTo.File("../output/Logs/clog-.txt", LogEventLevel.Debug, rollingInterval: RollingInterval.Day)
|
||||
.CreateLogger();
|
||||
builder.Logging.ClearProviders(); // 清空默认的日志记录器
|
||||
builder.Logging.AddConsole();
|
||||
builder.Logging.AddSerilog();
|
||||
builder.Services.AddSerilog();
|
||||
builder.Host.UseSerilog();
|
||||
logger.Information("启动项目");
|
||||
// Add services to the container.
|
||||
var giteaWebhookConfigModelList = builder.Configuration.GetSection("GiteaWebhookConfig").Get<List<GiteaWebhookConfigModel>>() ?? new List<GiteaWebhookConfigModel>();
|
||||
builder.Services.AddSingleton(giteaWebhookConfigModelList);
|
||||
|
|
@ -28,6 +53,7 @@ app.UseAuthorization();
|
|||
|
||||
app.MapControllers();
|
||||
app.UseMiddleware<GiteaWebhookConfigMiddleware>();
|
||||
app.UseSerilogRequestLogging();
|
||||
app.MapGet("/", () =>
|
||||
{
|
||||
return "请求成功";
|
||||
|
|
@ -46,7 +72,7 @@ app.MapGet("/system", () =>
|
|||
long memoryUsage = currentProcess.WorkingSet64;
|
||||
return new
|
||||
{
|
||||
msg = $"系统版本:{InformationalVersion},启动时间:{startDateTime.ToString("yyyy-MM-dd HH:mm:ss")},已安全运行时间:{DateTime.Now.Subtract(startDateTime).TotalMinutes.ToString("#.##")}分钟",
|
||||
msg = $"系统版本:{InformationalVersion},启动时间:{startDateTime.ToString("yyyy-MM-dd HH:mm:ss")},已安全运行时间:{DateTime.Now.Subtract(startDateTime).TotalMinutes.ToString("0.##")}分钟",
|
||||
InformationalVersion,
|
||||
startDateTime,
|
||||
MemoryUsage = $"{memoryUsage / (1024.0 * 1024.0):F2}MB",
|
||||
|
|
|
|||
|
|
@ -5,11 +5,45 @@
|
|||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
//日志
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{ "Name": "Console" },
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "../output/Logs/log-.txt",
|
||||
"rollingInterval": "Day",
|
||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
|
||||
"Destructure": [
|
||||
{
|
||||
"Name": "ToMaximumDepth",
|
||||
"Args": { "maximumDestructuringDepth": 4 }
|
||||
},
|
||||
{
|
||||
"Name": "ToMaximumStringLength",
|
||||
"Args": { "maximumStringLength": 100 }
|
||||
},
|
||||
{
|
||||
"Name": "ToMaximumCollectionCount",
|
||||
"Args": { "maximumCollectionCount": 10 }
|
||||
}
|
||||
],
|
||||
"Properties": {
|
||||
"Application": "Sample"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://*:5240"
|
||||
"Url": "http://*:82"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user