简体   繁体   中英

Logging to Azure Log Analytics custom fields using Serilog

I'm new to azure and also a rookie in .net, so this may be a noob question :) Anyway, I have successfully managed to log to Azure Log Analytics using this code snippet:

var loggerConfig = new LoggerConfiguration().
            Enrich.WithExceptionDetails().
            Enrich.WithApplicationInformation(serviceName).
            MinimumLevel.Debug().
            MinimumLevel.Override("Microsoft", LogEventLevel.Information).
            Enrich.FromLogContext().
            Destructure.ByTransforming<ExpandoObject>(JsonConvert.SerializeObject).
            //Enrich.WithProperty("ErrorMsg_CF","test").
            WriteTo.AzureAnalytics(workspaceId: "MyWorkSpaceID", 
                                   authenticationId: "MyAuthID",
                                   logName: "MyCustomLog_CL",
                                   restrictedToMinimumLevel: LogEventLevel.Debug)

And:

        Logging.Logger.Initialize(serviceName: "Logging Web Test v1.1");
        Logging.Logger.GetLogger().Log(LogLevel.Debug, "{ErrorMsg_CF}:{TraceID_CF}:{UserName_CF}", errorMsg,traceID,userName);

I have, in my Log Analytics custom log table, created 3 custom fields: "ErrorMsg_CF", "TraceID_CF" and "UserName_CF", and I want to either be able to directly log to these custom fields, or somehow split my errormsg into these three custom fields.

When I import a file, I see that the message is stored in the "RawData" field, and with that field I am able to use the custom field generator and create custom fields, and also let Log Analytics split the message into the fields for me. But, when I do it through my application, the message is stored in the "LogMEssage_s" field, and seems like it's not possible to create custom fields from that field.

So, anyone know how I can log to my custom fields from my application?

The default formatting configuration is a line by line event logging. ALN can be ingested using JSON so that will be the best way to format log entry in that way.

.WriteTo.DESTINATION(new CompactJsonFormatter(), OTHER OPTIONS HERE)

Serilog formatting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM