数据未进入日志表

时间:2017-05-17 05:48:40

标签: c# postgresql serilog

我正在使用Serilog.Sinks.PostgresqlNuget包在lon内写PostgreSQL但不写任何日志

IDictionary<string, ColumnWriterBase> columnWriters = new Dictionary<string, ColumnWriterBase>
{
  {"message", new RenderedMessageColumnWriter()},
  {"message_template", new MessageTemplateColumnWriter() },
  {"level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
  {"raise_date", new TimeStampColumnWriter() },
  {"exception", new ExceptionColumnWriter() },
  {"properties", new LogEventSerializedColumnWriter() },
  {"props_test", new PropertiesColumnWriter(NpgsqlDbType.Text) },
  {"machine_name", new SinglePropertyColumnWriter("MachineName", PropertyWriteMethod.Raw) }
};

2 个答案:

答案 0 :(得分:0)

此代码不应写入任何日志数据。本规范是启动和注册记录器的一部分。

string connectionstring = "User 
ID=serilog;Password=serilog;Host=localhost;Port=5432;Database=logs";

string tableName = "logs";

//Used columns (Key is a column name)
IDictionary<string, ColumnWriterBase> columnWriters = new Dictionary<string, ColumnWriterBase>
{
    {"message", new RenderedMessageColumnWriter() },
    {"message_template", new MessageTemplateColumnWriter() },
    {"level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
    {"raise_date", new TimeStampColumnWriter() },
    {"exception", new ExceptionColumnWriter() },
    {"properties", new LogEventSerializedColumnWriter() },
    {"props_test", new PropertiesColumnWriter(NpgsqlDbType.Text) },
    {"machine_name", new SinglePropertyColumnWriter("MachineName", PropertyWriteMethod.Raw) }
};

var logger = new LoggerConfiguration()
                    .WriteTo.PostgreSQL(connectionstring, tableName, columnWriters)
                    .CreateLogger();

之后您可以使用以下内容编写日志:

logger.ForContext("Context", "test")
                .Warning("THIS IS A WARN");

答案 1 :(得分:0)

我很高兴地说我喜欢这个问题 这是因为我在表格中添加了额外的列(Id自动增量) https://www.symbolsource.org/Public/Metadata/NuGet/Project/Npgsql/3.0.0-beta0001/Release/.NETFramework,Version%3Dv4.5/Npgsql/Npgsql/NpgsqlBinaryImporter.cs?ImageName=Npgsql 现在它对我来说很好