要存储在 Azure 存储中的控制台应用程序日志
Console application Logs to store in Azure storage
我正在使用 运行 在本地服务器上由任务计划程序触发的控制台应用程序。此控制台应用程序执行各种操作并需要记录这些操作。它会在每个 运行 和控制台应用程序 运行 每小时生成大约 200kb 的日志。
由于我们无法访问服务器,我打算将日志存储到 Azure。我阅读了有关 blob/table 存储的信息。
我想知道在 Azure 中存储日志的最佳策略是什么。
谢谢。
虽然您可以在 Azure 存储(Blob 和表)中写入日志记录数据,但如果您使用 Azure Application Insights
来记录这些数据实际上会更有意义。
我最近对我构建的控制台应用程序做了同样的事情。我发现它非常简单。
我在我的 Azure 订阅中创建了 App Insight 资源并获得了 instrumentation key
。然后我安装了 App Insights SDK
并在我的项目中引用了适当的命名空间。
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
这是我初始化遥测客户端的方式:
var appSettingsReader = new AppSettingsReader();
var appInsightsInstrumentationKey = (string)appSettingsReader.GetValue("AppInsights.InstrumentationKey", typeof(string));
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = appInsightsInstrumentationKey;
telemetryClient = new TelemetryClient(configuration);
telemetryClient.InstrumentationKey = appInsightsInstrumentationKey;
为了记录跟踪数据,我只是做了以下操作:
TraceTelemetry telemetry = new TraceTelemetry(message, SeverityLevel.Verbose);
telemetryClient.TrackTrace(telemetry);
为了记录错误数据,我只是做了以下操作:
catch (Exception excep)
{
var message = string.Format("Error. {0}", excep.Message);
ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(excep);
telemetryClient.TrackException(exceptionTelemetry);
telemetryClient.Flush();
Task.Delay(5000).Wait();//Wait for 5 seconds before terminating the application
}
请记住一件事:确保在终止应用程序之前等待一段时间(5 秒就足够了)刷新数据。
如果您仍然热衷于将日志写入 Azure 存储,根据您使用的日志记录库,您会找到可以直接写入 Azure 存储的合适适配器。
例如,有一个用于 Azure 表的 NLog 目标:https://github.com/harouny/NLog.Extensions.AzureTableStorage(尽管这个项目没有得到积极维护)。
我正在使用 运行 在本地服务器上由任务计划程序触发的控制台应用程序。此控制台应用程序执行各种操作并需要记录这些操作。它会在每个 运行 和控制台应用程序 运行 每小时生成大约 200kb 的日志。
由于我们无法访问服务器,我打算将日志存储到 Azure。我阅读了有关 blob/table 存储的信息。
我想知道在 Azure 中存储日志的最佳策略是什么。
谢谢。
虽然您可以在 Azure 存储(Blob 和表)中写入日志记录数据,但如果您使用 Azure Application Insights
来记录这些数据实际上会更有意义。
我最近对我构建的控制台应用程序做了同样的事情。我发现它非常简单。
我在我的 Azure 订阅中创建了 App Insight 资源并获得了 instrumentation key
。然后我安装了 App Insights SDK
并在我的项目中引用了适当的命名空间。
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
这是我初始化遥测客户端的方式:
var appSettingsReader = new AppSettingsReader();
var appInsightsInstrumentationKey = (string)appSettingsReader.GetValue("AppInsights.InstrumentationKey", typeof(string));
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = appInsightsInstrumentationKey;
telemetryClient = new TelemetryClient(configuration);
telemetryClient.InstrumentationKey = appInsightsInstrumentationKey;
为了记录跟踪数据,我只是做了以下操作:
TraceTelemetry telemetry = new TraceTelemetry(message, SeverityLevel.Verbose);
telemetryClient.TrackTrace(telemetry);
为了记录错误数据,我只是做了以下操作:
catch (Exception excep)
{
var message = string.Format("Error. {0}", excep.Message);
ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(excep);
telemetryClient.TrackException(exceptionTelemetry);
telemetryClient.Flush();
Task.Delay(5000).Wait();//Wait for 5 seconds before terminating the application
}
请记住一件事:确保在终止应用程序之前等待一段时间(5 秒就足够了)刷新数据。
如果您仍然热衷于将日志写入 Azure 存储,根据您使用的日志记录库,您会找到可以直接写入 Azure 存储的合适适配器。
例如,有一个用于 Azure 表的 NLog 目标:https://github.com/harouny/NLog.Extensions.AzureTableStorage(尽管这个项目没有得到积极维护)。