我正在尝试将nlog的输出写入存储表。我也在控制台上打印它,工作正常。但是,表格不会在我想要记录输出的地方创建。
以下是我的app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="StorageConnectionString" value="value" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
nlog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
<targets>
<target name="console" xsi:type="Console" layout="Console output: Debug Error caught" />
<target name="eventlog" xsi:type="EventLog" log="Application" />
</targets>
<rules>
<logger name="*" minlevel="Warn" writeTo="console" />
<logger name="*" levels="Error,Fatal,Warn" writeTo="eventlog" />
</rules>
</nlog>
课程计划:
class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
ConfigurationItemFactory.Default.Targets
.RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));
try
{
logger.Trace("Sample trace message");
logger.Debug("Sample debug message");
logger.Info("Sample informational message");
logger.Warn("Sample warning message");
logger.Error("Sample error message");
logger.Fatal("Sample fatal error message");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
自定义目标类:
[Target("MyFirst")]
public sealed class MyFirstTarget : TargetWithLayout
{
public MyFirstTarget()
{
}
protected override void Write(LogEventInfo logEvent)
{
string logMessage = this.Layout.Render(logEvent);
if (logEvent.Parameters != null && logEvent.Parameters.Count() > 1)
SendTheMessageToRemoteHost(logEvent.Parameters[0] as string, logEvent.Message);
}
private void SendTheMessageToRemoteHost(string id, string body)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("SampleLogTable2");
table.CreateIfNotExists();
LogEntry le = new LogEntry();
le.Message = body;
le.id = id;
TableOperation insert = TableOperation.Insert(le);
table.Execute(insert);
}
}
存储表类:
public class LogEntry : TableEntity
{
public LogEntry()
{
var now = DateTime.UtcNow;
PartitionKey = string.Format("{0:yyyy-MM}", now);
RowKey = string.Format("{0:dd HH:mm:ss.fff}-{1}", now, Guid.NewGuid());
}
#region Table columns
public string id { get; set; }
public string Message { get; set; }
public string Level { get; set; }
public string LoggerName { get; set; }
public string RoleInstance { get; set; }
public string DeploymentId { get; set; }
public string StackTrace { get; set; }
#endregion
}
}
couput出现在控制台上。
但是,存储表(自定义目标)无法到达。