简体   繁体   中英

.cscfg file gives error when i specify <Setting name=“serilog:write-to:AzureDocumentDB.endpointUrl” />

When I publish my API project to Azure here is the error what i'm getting. 在此处输入图片说明

Is there any way to resolve this issue and here is the code and the problem comes when this symbol " : " is init.

在此处输入图片说明

Here are some more details.

  1. It's a web API Project
  2. Version 4.6
  3. Locally it's running without any issues but when it comes to release automation I should be able to change the value of endpointurl , key, and TTL manually so that I need to modify .csfg and .csdef file environment to environment. When i do that .csdef does not support that colon mark ":" so the build fails.

在此处输入图片说明

  • Expecting : Build is successful so that the log works as expected.
  • Actual : Build fails and log is not working as expected.

It seems this is not supported.

ServiceDefinition:NamedElementNameString doesn't allow ':' (colon) in name

So that I implemented custom configuration values and extract the values at run-time when the logger is initialized.

Here is the implementation.

.cscfg, .csdef, and web.config contains

<add key="LogEndpointUrl" value="xxxxxx/" />
<add key="LogAuthorizationKey" value="xxxxxxxxxxxxxxx=" />
<add key="LogTTL" value="1" />

When initializing got the values as follows from web.config

var endpoint = Common.Configuration.GetSetting(Constants.AppSettings.LogEndpointUrl);
var authorizationKey = Common.Configuration.GetSetting(Constants.AppSettings.LogAuthorizationKey);
int ttl = (int)Convert.ToInt64((Common.Configuration.GetSetting(Constants.AppSettings.LogTTL)));

And then

Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().WriteTo.AzureDocumentDB(endpoint, authorizationKey,timeToLive: ttl).CreateLogger();

// Used to debug serilog itself and confirm it is writing entries to document db
Serilog.Debugging.SelfLog.Enable(Console.Out);
var errorOrInformation = new Dictionary<string, string>();
    errorOrInformation.Add(Constants.LoggingProperties.PartitionKey, logMetadata.PartitionKey);
    errorOrInformation.Add(Constants.LoggingProperties.RowKey, logMetadata.RowKey);
//Add as many items as you want

Log.Verbose("Log Information Message {Information}", errorOrInformation);
// Also good idea to force flush of log entries before the app ends
Log.CloseAndFlush();

As you mentioned that ServiceDefinition:NamedElementNameString doesn't allow ':' (colon) in name. But we could add it with Azure friendly name. Then we could get it with following code. I also do a demo on my side, it works as expected.

var endpoint = RoleEnvironment.GetConfigurationSettingValue("endpointUrl");
var authorizationKey = RoleEnvironment.GetConfigurationSettingValue("authorizationKey");
var logger = new LoggerConfiguration()
             .WriteTo.Console() //if no writeto.console there is no document in documentdb
             .WriteTo.AzureDocumentDB(endpoint, authorizationKey)
             .CreateLogger();
logger.Information("Tom Test");//log demo 

About .csdef configuration please refer to the screenshot. We could get more information from Configure Azure cloud service roles with Visual Studio

在此处输入图片说明

Check from Azure Portal:

在此处输入图片说明

Related serilog sdk

在此处输入图片说明

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