我尝试通过Serilog登录ES,并且我无法使用浓缩功能。我想在app.config中包含值,以利用部署更新的信息。
ES / Rolling文件中没有显示任何字段。我已经验证了这些值是从app.config中按预期读取的。
设置:控制台应用程序,静态记录器实例,Enrich.With<>
public class SettingsEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var settings = new SettingsFromConfigFile();
propertyFactory.CreateProperty("Environment", settings.Environment);
propertyFactory.CreateProperty("SuiteName", settings.SuiteName);
propertyFactory.CreateProperty("ComponentName", settings.ComponentName);
propertyFactory.CreateProperty("ReleaseNumber", settings.ReleaseNumber);
}
}
public class SettingsFromConfigFile
{
public SettingsFromConfigFile()
{
Environment = ConfigurationManager.AppSettings["Environment"];
SuiteName = ConfigurationManager.AppSettings["SuiteName"];
ComponentName = ConfigurationManager.AppSettings["ComponentName"];
LogFileDirectory = ConfigurationManager.AppSettings["LogFileDirectory"];
EsLoggingUrl = ConfigurationManager.AppSettings["EsLoggingUrl"];
ReleaseNumber = ConfigurationManager.AppSettings["ReleaseNumber"];
FileName = $"{LogFileDirectory}{SuiteName}.{ComponentName}.log";
EsLoggingUri = new Uri(EsLoggingUrl);
ElasticsearchSinkOptions = new ElasticsearchSinkOptions(EsLoggingUri);
}
public string LogFileDirectory { get; set; }
public string ReleaseNumber { get; set; }
public string ComponentName { get; set; }
public string SuiteName { get; set; }
public string FileName { get; set; }
public string EsLoggingUrl { get; set; }
public Uri EsLoggingUri { get; set; }
public ElasticsearchSinkOptions ElasticsearchSinkOptions { get; set; }
public string Environment { get; set; }
}
public class StandardLoggerConfigurator
{
public static ILogger GetEnrichedLogger()
{
var settings = new SettingsFromConfigFile();
return new LoggerConfiguration()
.Enrich.With<SettingsEnricher>()
//.Enrich.FromLogContext()
.WriteTo.RollingFile(settings.FileName)
.WriteTo.Elasticsearch(settings.ElasticsearchSinkOptions)
.CreateLogger();
}
}
internal class Program
{
private static void Main(string[] args)
{
Log.Logger = StandardLoggerConfigurator.GetEnrichedLogger();
Log.Logger.Information("Starting service!!!!!");