diff --git a/src/CodeRelease/CodeRelease/BLL/LinuxExecuteCommand.cs b/src/CodeRelease/CodeRelease/BLL/LinuxExecuteCommand.cs index 403b2b0..a629d99 100644 --- a/src/CodeRelease/CodeRelease/BLL/LinuxExecuteCommand.cs +++ b/src/CodeRelease/CodeRelease/BLL/LinuxExecuteCommand.cs @@ -1,4 +1,11 @@ -using System.Diagnostics; +using CodeRelease.Model; + +using Serilog; + +using System; +using System.Diagnostics; + +using static System.Runtime.InteropServices.JavaScript.JSType; namespace CodeRelease.BLL { @@ -32,6 +39,7 @@ namespace CodeRelease.BLL // 绑定输出和错误数据接收事件 process.OutputDataReceived += new DataReceivedEventHandler((sender, e) => { + Log.Logger.Information("linux执行出错,命令{command},sender:{sender}Output:{e}", command, sender, e); if (!string.IsNullOrEmpty(e.Data)) { Console.WriteLine($"Output: {e.Data}"); @@ -41,6 +49,7 @@ namespace CodeRelease.BLL process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => { + Log.Logger.Information("linux执行出错,命令{command},sender:{sender}Output:{e}", command, sender, e); if (!string.IsNullOrEmpty(e.Data)) { Console.WriteLine($"Error: {e.Data}"); @@ -59,13 +68,14 @@ 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}"); exitCode = process.ExitCode; } } catch (Exception ex) { - Console.WriteLine($"Exception: {ex.Message}"); + Log.Logger.Information($"执行出现异常结束 {ex.Message}"); } return new { exitCode, outputDataReceived, errorDataReceived }; } diff --git a/src/CodeRelease/CodeRelease/CodeRelease.csproj b/src/CodeRelease/CodeRelease/CodeRelease.csproj index d9e21ad..eed5ec8 100644 --- a/src/CodeRelease/CodeRelease/CodeRelease.csproj +++ b/src/CodeRelease/CodeRelease/CodeRelease.csproj @@ -8,6 +8,12 @@ + + + + + + diff --git a/src/CodeRelease/CodeRelease/Controllers/PublishController.cs b/src/CodeRelease/CodeRelease/Controllers/PublishController.cs index 21988ca..0166f8f 100644 --- a/src/CodeRelease/CodeRelease/Controllers/PublishController.cs +++ b/src/CodeRelease/CodeRelease/Controllers/PublishController.cs @@ -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 /// public async Task ReleaseAction([FromBody] object data, [FromServices] GiteaWebhookConfigModel giteaWebhookConfig) { + Log.Logger.Information("执行发布接口,参数{data}", data); JsonSerializerSettings settings = new JsonSerializerSettings { //MaxDepth = 64 // 设置一个足够大的值,默认值是 32 @@ -85,18 +88,11 @@ 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("{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); diff --git a/src/CodeRelease/CodeRelease/Program.cs b/src/CodeRelease/CodeRelease/Program.cs index 49b1240..d7d3b02 100644 --- a/src/CodeRelease/CodeRelease/Program.cs +++ b/src/CodeRelease/CodeRelease/Program.cs @@ -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>() ?? new List(); builder.Services.AddSingleton(giteaWebhookConfigModelList); @@ -28,6 +53,7 @@ app.UseAuthorization(); app.MapControllers(); app.UseMiddleware(); +app.UseSerilogRequestLogging(); app.MapGet("/", () => { return "请求成功"; diff --git a/src/CodeRelease/CodeRelease/appsettings.json b/src/CodeRelease/CodeRelease/appsettings.json index 339d1d2..a5183f6 100644 --- a/src/CodeRelease/CodeRelease/appsettings.json +++ b/src/CodeRelease/CodeRelease/appsettings.json @@ -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" } } },