centos

Centos Swagger如何进行日志管理

小樊
41
2025-02-18 23:57:24
栏目: 智能运维

在CentOS系统中,Swagger的日志管理可以通过配置log4net来实现。以下是具体的步骤:

1. 创建配置文件

首先,创建一个新的文件夹来存放Swagger的配置文件,例如 CfgFile

2. 新建XML配置文件

CfgFile 文件夹中新建一个名为 log4net.Config 的XML配置文件,并添加以下内容:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!-- Define some output appenders -->
  <appender name="rollingAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logs/swagger.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppenderMinimalLock" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyyMMdd".txt" />
    <maxSizeRollBackups value="20" />
    <maximumFileSize value="3MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>

  <!-- Set the appender to the root logger -->
  <root level="ALL">
    <level value="ALL" />
    <appender-ref ref="rollingAppender" />
  </root>
</log4net>

3. 修改Swagger配置

在Swagger的配置文件(通常是 Program.cs)中,增加log4net的配置项:

using System;
using System.IO;
using System.Reflection;
using log4net;
using log4net.Config;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Options;

namespace YourNamespace
{
    public class Program
    {
        static void Main(string[] args)
        {
            var logPath = Path.Combine(AppContext.BaseDirectory, "logs");
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            var file = Path.Combine(AppContext.BaseDirectory, "WebApi.xml");
            var option = new OpenApiOptions
            {
                SwaggerDoc("v1", new OpenApiInfo { Title = "系统后端API接口文档", Version = "v1" }),
                IncludeXmlComments(file, true)
            };

            builder.Services.AddSwaggerGen(option =>
            {
                option.SwaggerDoc("v1", new OpenApiInfo { Title = "系统后端API接口文档", Version = "v1" });
                var logger = LogManager.GetLogger(typeof(Program));
                logger.Info("Swagger configuration initialized.");
            });

            builder.Logging.AddLog4Net(cfgFile: "CfgFile/log4net.Config");

            var host = builder.Build();
            host.Run();
        }

        private static void IncludeXmlComments(string xmlFile, bool include)
        {
            var config = new ExeConfigurationFileMap { ExeConfigFilename = xmlFile };
            var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = xmlFile };
            var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            var logger = LogManager.GetLogger(typeof(Program));
            logger.Info($"Including XML comments: {include}");
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTrademarkAttribute), new AssemblyTrademarkAttribute("Your Trademark"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCompanyAttribute), new AssemblyCompanyAttribute("Your Company"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTitleAttribute), new AssemblyTitleAttribute("Your Application"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyDescriptionAttribute), new AssemblyDescriptionAttribute("Your Application Description"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyVersionAttribute), new AssemblyVersionAttribute("1.0.0.0"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCopyrightAttribute), new AssemblyCopyrightAttribute("Copyright © Your Company 2023"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTrademarkAttribute), new AssemblyTrademarkAttribute("Your Trademark"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCompanyAttribute), new AssemblyCompanyAttribute("Your Company"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTitleAttribute), new AssemblyTitleAttribute("Your Application"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyDescriptionAttribute), new AssemblyDescriptionAttribute("Your Application Description"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyVersionAttribute), new AssemblyVersionAttribute("1.0.0.0"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCopyrightAttribute), new AssemblyCopyrightAttribute("Copyright © Your Company 2023"));
            config.Save(configFileMap.ExeConfigFilename);
        }
    }
}

4. 配置log4net

Program.cs 中,添加以下代码来配置log4net:

using System;
using System.IO;
using System.Reflection;
using log4net;
using log4net.Config;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Options;

namespace YourNamespace
{
    public class Program
    {
        static void Main(string[] args)
        {
            var logPath = Path.Combine(AppContext.BaseDirectory, "logs");
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            var file = Path.Combine(AppContext.BaseDirectory, "WebApi.xml");
            var option = new OpenApiOptions
            {
                SwaggerDoc("v1", new OpenApiInfo { Title = "系统后端API接口文档", Version = "v1" }),
                IncludeXmlComments(file, true)
            };

            builder.Services.AddSwaggerGen(option =>
            {
                option.SwaggerDoc("v1", new OpenApiInfo { Title = "系统后端API接口文档", Version = "v1" });
                var logger = LogManager.GetLogger(typeof(Program));
                logger.Info("Swagger configuration initialized.");
            });

            builder.Logging.AddLog4Net(cfgFile: "CfgFile/log4net.Config");

            var host = builder.Build();
            host.Run();
        }

        private static void IncludeXmlComments(string xmlFile, bool include)
        {
            var config = new ExeConfigurationFileMap { ExeConfigFilename = xmlFile };
            var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = xmlFile };
            var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            var logger = LogManager.GetLogger(typeof(Program));
            logger.Info($"Including XML comments: {include}");
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTrademarkAttribute), new AssemblyTrademarkAttribute("Your Trademark"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCompanyAttribute), new AssemblyCompanyAttribute("Your Company"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTitleAttribute), new AssemblyTitleAttribute("Your Application"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyDescriptionAttribute), new AssemblyDescriptionAttribute("Your Application Description"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyVersionAttribute), new AssemblyVersionAttribute("1.0.0.0"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCopyrightAttribute), new AssemblyCopyrightAttribute("Copyright © Your Company 2023"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTrademarkAttribute), new AssemblyTrademarkAttribute("Your Trademark"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCompanyAttribute), new AssemblyCompanyAttribute("Your Company"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyTitleAttribute), new AssemblyTitleAttribute("Your Application"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyDescriptionAttribute), new AssemblyDescriptionAttribute("Your Application Description"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyVersionAttribute), new AssemblyVersionAttribute("1.0.0.0"));
            config.AssemblyInfo.SetCustomAttribute(typeof(AssemblyCopyrightAttribute), new AssemblyCopyrightAttribute("Copyright © Your Company 2023"));
            config.Save(configFileMap.ExeConfigFilename);
        }
    }
}

通过以上步骤,你可以在CentOS系统中使用log4net来管理Swagger的日志,并将日志记录到指定的文件中。

0
看了该问题的人还看了