如何在asp.net core 2.1应用程序中为postgres数据库配置log4net?

时间:2018-10-09 17:49:56

标签: postgresql log4net asp.net-core-2.1 log4net-configuration

我有一个log4net.config文件,内容如下:

<log4net>
  <appender name="PostgresAppender" type="App.PostgresAppender, App">
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="PostgresAppender" />
  </root>
</log4net>

PostgresAppender

public class PostgresAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
        using (NpgsqlConnection conn = new NpgsqlConnection(Startup.Configuration.GetConnectionString("Logging")))
        {
            conn.Open();
            using (NpgsqlCommand command = new NpgsqlCommand("insert into public.logs(app_name,thread,level,location,message,log_date,exception) values(:app_name,:thread,:level,:location,:message,:log_date,:exception)", conn))
            {
                var appName = command.CreateParameter();
                appName.Direction = System.Data.ParameterDirection.Input;
                appName.DbType = System.Data.DbType.String;
                appName.ParameterName = ":app_name";
                appName.Value = loggingEvent.LookupProperty("AppName");
                command.Parameters.Add(appName);

                var thread = command.CreateParameter();
                thread.Direction = System.Data.ParameterDirection.Input;
                thread.DbType = System.Data.DbType.String;
                thread.ParameterName = ":thread";
                thread.Value = loggingEvent.ThreadName;
                command.Parameters.Add(thread);

                var level = command.CreateParameter();
                level.Direction = System.Data.ParameterDirection.Input;
                level.DbType = System.Data.DbType.String;
                level.ParameterName = ":level";
                level.Value = loggingEvent.Level;
                command.Parameters.Add(level);

                var location = command.CreateParameter();
                location.Direction = System.Data.ParameterDirection.Input;
                location.DbType = System.Data.DbType.String;
                location.ParameterName = ":location";
                location.Value = loggingEvent.LocationInformation.FullInfo;
                command.Parameters.Add(location);

                var message = command.CreateParameter();
                message.Direction = System.Data.ParameterDirection.Input;
                message.DbType = System.Data.DbType.String;
                message.ParameterName = ":message";
                message.Value = loggingEvent.RenderedMessage;
                command.Parameters.Add(message);

                var log_date = command.CreateParameter();
                log_date.Direction = System.Data.ParameterDirection.Input;
                log_date.DbType = System.Data.DbType.DateTime2;
                log_date.ParameterName = ":log_date";
                log_date.Value = loggingEvent.TimeStamp;
                command.Parameters.Add(log_date);

                var exception = command.CreateParameter();
                exception.Direction = System.Data.ParameterDirection.Input;
                exception.DbType = System.Data.DbType.String;
                exception.ParameterName = ":exception";
                exception.Value = loggingEvent.GetExceptionString();
                command.Parameters.Add(exception);

                command.ExecuteNonQuery();
            }
            conn.Close();
        }
    }
}

appsettings.json中,我像这样配置log4net:

"Log4NetConfigFile": {
    "Name": "log4net.config"
  }

然后我尝试通过在program.cs中设置记录器来进行记录:

 public static readonly ILog _log = LogManager.GetLogger(typeof(Program));

并在其他类似地方使用它:

log4net.GlobalContext.Properties["App"] = Startup.Configuration.GetSection("Log4NetConfigFile");
Program._log.Info("It works my friends");

但是我根本无法记录任何内容。

  

我尝试通过定位到此tutorial来进行设置,但是   我从xml app.config进行转换时遇到的最大麻烦   / web.config转到我的appsettings.jsonlog4net.config。我的   我的postgres数据库的连接字符串也应该没问题。我设定了   PostgresAppender中的断点,但在   触发日志,所以我认为它必须与设置有关。什么时候   您需要更多信息,请让我知道。

0 个答案:

没有答案