有没有办法解决这个问题,这是代码,当这个符号" :"是init。
以下是一些更多细节。
答案 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
获取更多信息从Azure门户检查:
相关的serilog sdk