当我指定<setting name =“serilog:write-to:AzureDocumentDB.endpointUrl”>时,.cscfg文件会出错

时间:2018-01-25 04:50:01

标签: c# azure web-config azure-cosmosdb serilog

当我将我的API项目发布到Azure时,这是我得到的错误。 enter image description here

有没有办法解决这个问题,这是代码,当这个符号&#34; &#34;是init。

enter image description here

以下是一些更多细节。

  1. 这是一个Web API项目
  2. 版本4.6
  3. 本地运行没有任何问题,但是当涉及到发布自动化时,我应该能够手动更改endpointurl,key和TTL的值,以便我需要修改.csfg和.csdef文件环境到环境。当我这样做.csdef不支持冒号和#34;:&#34;所以构建失败。
  4. enter image description here

    • 期望:构建成功,以便日志按预期工作。
    • 实际:构建失败,日志未按预期工作。

2 个答案:

答案 0 :(得分:1)

似乎不支持。

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

答案 1 :(得分:1)

正如您所提到的,ServiceDefinition:NamedElementNameString在名称中不允许使用':'(冒号)。但我们可以使用Azure友好名称添加它。然后我们可以使用以下代码获取它。我也在我身边做一个演示,它按预期工作。

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

获取更多信息

enter image description here

从Azure门户检查:

enter image description here

相关的serilog sdk

enter image description here