c# 将 NLog 与 Mongo 一起使用
c# Use NLog with Mongo
我会用 NLog 和 MongoDB 记录我的数据。我这样配置 NLog.config:
<extensions>
<add assembly="NLog.Mongo"/>
</extensions>
...
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${Id}"/>
<field name="ts" layout="${Timestamp}" bsonType="DateTime"/>
<field name="cap" layout="${ApplicationCaller}" />
</target>
但是我如何在我的日志中传递值 ID、时间戳、ApplicationCaller??
我试试这个代码:
public void LogExceptionOnMongo(string callIdentifier, string applicationCaller)
{
var _myLogger = LogManager.GetLogger("mongo");
var logEventInfo = new LogEventInfo(LogLevel.Fatal, "", "Exception");
logEventInfo.Properties["CallIdentifier"] = callIdentifier;
logEventInfo.Properties["TimeStamp"] = DateTime.UtcNow;
logEventInfo.Properties["ApplicationCaller"] = applicationCaller;
_myLogger.Log(logEventInfo);
}
和这个配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
但是我在mongo中获得了这个登录(我不会圈出数据):
请问有人能帮帮我吗??谢谢你,对不起我的英语
必须更改代码才能适应这一点。有关详细信息,请参阅 Issue。
新 属性 includeEventProperties
现在可以配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
includeEventProperties="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
就用ver.来自 nuget 的 4.6.0.68:https://www.nuget.org/packages/NLog.Mongo
今天早上我尝试使用新版本的库,这是新配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400"
includeEventProperties="false">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTime"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
使用新标签 includeEventProperties=false 并在 class MongoTarget 中使用新的 属性 并且工作正常。这是结果:
非常感谢,辛苦了
我会用 NLog 和 MongoDB 记录我的数据。我这样配置 NLog.config:
<extensions>
<add assembly="NLog.Mongo"/>
</extensions>
...
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${Id}"/>
<field name="ts" layout="${Timestamp}" bsonType="DateTime"/>
<field name="cap" layout="${ApplicationCaller}" />
</target>
但是我如何在我的日志中传递值 ID、时间戳、ApplicationCaller?? 我试试这个代码:
public void LogExceptionOnMongo(string callIdentifier, string applicationCaller)
{
var _myLogger = LogManager.GetLogger("mongo");
var logEventInfo = new LogEventInfo(LogLevel.Fatal, "", "Exception");
logEventInfo.Properties["CallIdentifier"] = callIdentifier;
logEventInfo.Properties["TimeStamp"] = DateTime.UtcNow;
logEventInfo.Properties["ApplicationCaller"] = applicationCaller;
_myLogger.Log(logEventInfo);
}
和这个配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
但是我在mongo中获得了这个登录(我不会圈出数据):
请问有人能帮帮我吗??谢谢你,对不起我的英语
必须更改代码才能适应这一点。有关详细信息,请参阅 Issue。
新 属性 includeEventProperties
现在可以配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
includeEventProperties="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
就用ver.来自 nuget 的 4.6.0.68:https://www.nuget.org/packages/NLog.Mongo
今天早上我尝试使用新版本的库,这是新配置:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400"
includeEventProperties="false">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTime"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
使用新标签 includeEventProperties=false 并在 class MongoTarget 中使用新的 属性 并且工作正常。这是结果: