.cscfg 文件在我指定 <Setting name="serilog:write-to:AzureDocumentDB.endpointUrl" /> 时出错

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

当我将我的 API 项目发布到 Azure 时,这是我遇到的错误。

有什么办法可以解决这个问题,这里是代码,当这个符号“:”初始化时,问题就来了。

这里有更多详细信息。

  1. 这是一个网络 API 项目
  2. 版本 4.6
  3. 本地 运行 没有任何问题,但在发布自动化方面,我应该能够手动更改 endpointurl 、密钥和 TTL 的值,以便我需要修改 .csfg 和 .csdef 文件环境到环境。当我这样做时,.csdef 不支持冒号标记“:”,因此构建失败。

好像不支持。

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

因此我实现了自定义配置值并在 运行 初始化记录器时提取值。

这是实现。

.cscfg、.csdef 和 web.config 包含

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

初始化时得到的值如下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)));

然后

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();

如您所述,ServiceDefinition:NamedElementNameString 不允许在名称中使用“:”(冒号)。但我们可以使用 Azure 友好名称添加它。然后我们可以用下面的代码得到它。我自己也做了一个demo,效果和预期的一样。

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 

.csdef配置请参考截图。 我们可以从 Configure Azure cloud service roles with Visual Studio

获得更多信息

从 Azure 门户检查:

相关serilog sdk