markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

How to add client IP address or any other custom fields? #152

Closed fairking closed 3 years ago

fairking commented 3 years ago

I know I can pass many other request related data to the log. Bellow is an example how to setup email log:

var mailTarget = new AsyncTargetWrapper
                        {
                            Name = "asyncMail",
                            WrappedTarget = new NLog.MailKit.MailTarget("mail")
                            {
                                SmtpServer = settings.Smtp.Host,
                                SmtpPort = settings.Smtp.Port,
                                SmtpUserName = settings.Smtp.Username,
                                SmtpPassword = settings.Smtp.Password,
                                EnableSsl = settings.Smtp.UseSsl,
                                SmtpAuthentication = NLog.MailKit.SmtpAuthenticationMode.Basic,
                                From = settings.Smtp.EmailAddress,
                                To = settings.Logging.EmailTo, //AppConfigService.LogsEmail,
                                Subject = "${level} from ${var:instance_name} ${var:version} ${message}",
                                Body = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}|${level:uppercase=true}|${logger:shortName=True}|${exception:format=Type}|${message}${newline}${newline}"
                                + "LOG DETAILS:${newline}${exception:format=ToString}${newline}${newline}"
                                + "INFORMATION:${newline}User: ${aspnet-user-identity},${newline}IP: ${local-ip},${newline}Version: ${assembly-version:type=Informational},${newline}"
                                    + "Environment: ${environment:variable=ASPNETCORE_ENVIRONMENT},${newline}Instance: ${var:instance_name},${newline}"
                                    + "Server Url: ${var:server_url},${newline}Client Url: ${var:client_url},${newline}Db type: ${var:db_type},${newline}"
                                    + "Db dialect: ${var:db_dialect},${newline}Db legacy: ${var:db_legacy},${newline}"
                                    + "Url: ${aspnet-request-url:IncludePort=true:IncludeQueryString=true},${newline}${newline}${newline}",
                            },
                        };

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.

fairking commented 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.