Closed fairking closed 3 years ago
Ok. Looks like I have found a solution:
var cfg = new NLog.Config.LoggingConfiguration();
cfg.Variables.Add("instance_name", settings.Settings.AppInstanceName);
cfg.Variables.Add("company_name", settings.Settings.CompanyName);
var elasticSearchTarget = new AsyncTargetWrapper {
Name = "asyncElasticsearch",
QueueLimit = 5000,
BatchSize = 100,
TimeToSleepBetweenBatches = 5000,
WrappedTarget = new ElasticSearchTarget()
{
Name = "elasticsearch",
Uri = AppConfigService.ElasticSearchUrl,
Index = "myapp-${var:version_number}-${date:format=yyyy.MM.dd}",
RequireAuth = true,
Username = "",
Password = "",
},
};
var elastic = (elasticSearchTarget.WrappedTarget as ElasticSearchTarget);
elastic.Fields.Add(new Field() { Name = "instance_name", Layout = "${var:instance_name}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "company_name", Layout = "${var:company_name}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "version", Layout = "${assembly-version:type=Informational}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "aspnet_user", Layout = "${aspnet-user-identity}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "local_ip", Layout = "${local-ip}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "environment", Layout = "${environment:variable=ASPNETCORE_ENVIRONMENT}", LayoutType = typeof(string) });
elastic.Fields.Add(new Field() { Name = "url", Layout = "${aspnet-request-url:IncludePort=true:IncludeQueryString=true}", LayoutType = typeof(string) });
cfg.AddRule(tdzMinLevel, LogLevel.Fatal, elasticSearchTarget);
NLog.LogManager.KeepVariablesOnReload = true;
NLog.LogManager.ThrowConfigExceptions = true;
NLog.LogManager.Configuration = cfg;
Thanks you guys.
I know I can pass many other
request
related data to the log. Bellow is an example how to setup email log:But there is missing documentation how to add those extra fields to the elastic search like
${aspnet-user-identity}
,${local-ip}
,${assembly-version:type=Informational}
etc.? At the moment by default I can only see Exception details in my elastic logs, nothing more.Could you please someone provide some example? Would be nice to have it in Wiki too for other users. Thanks.