我在我的專案中設定了應用程式洞察,它作業正常,它將資料發送到 Azure 沒有任何問題,現在我嘗試使用 System.Diagnostics.SourceTrace 將一些跟蹤日志發送到發送到 azure 的遙測中,這是在內部實作的Webhost 應用程式中參考的 nuget 包(此 nuget 包不包含對應用程式洞察的參考),問題是......出于某種原因,它只是沒有到達該代碼,好吧,它確實存在,也沒有同時,當我在輸出視窗中除錯時,我可以看到當它點擊 Sytem.Diagnostics.TraceEvent() 方法時創建了一個事件,但它顯示如下
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Message","time":"2021-09-01T22:43:18.7652108Z","tags":{"ai.cloud.roleInstance":
這讓我覺得由于某種原因,遙測客戶端丟失了對儀器密鑰或類似東西的參考,我不知道如何解決這個問題,因為它只發生在那里。
編輯:這是我們設定跟蹤源的方式,此代碼位于 webhost 應用程式的 web.config 檔案中,該檔案參考另一個專案,該專案參考了發生日志記錄的 nuget 包。
<source name="MySource" switchValue="Error, Information, Warning">
<listeners>
<add name="AppInsights" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
</listeners>
</source>
我除錯了日志記錄發生的類,當我評估遙測配置物件時,它缺少檢測鍵(這很奇怪,因為大多數遙測作業正常)
這是我們設定遙測客戶端的代碼:
public void Initialize()
{
if (_initialized) return;
lock (_initializationLock)
{
if (_initialized) return;
var iKey = ApplicationInsightsConfiguration.InstrumentationKey;
//Call this even if ikey is null or empty
MonitoringSettings = new Settings(iKey);
//If we don't have a key we can't do anything
if (string.IsNullOrEmpty(iKey))
{
Logger.Info($"No Application Insights telemetry key is available (Production flag: {SystemSettings.IsProductionServer})");
TelemetryConfiguration.Active.DisableTelemetry = true;
return;
}
//Set telemetry key
TelemetryConfiguration.Active.InstrumentationKey = iKey;
//Set up custom telemetry initializers
//We need to initialize it before we send the non-prod custom event, so that the event will contain all required info
SetUpTelemetryInitializers();
//Disable telemetry reporting if it is not production instance
//If overridden in web.ApplicationInsightsConfiguration explicitly, allow telemetry reporting
if (ApplicationInsightsConfiguration.ForceSendTelemetry)
{
Client.TrackEvent("ForceSendTelemetry enabled.");
}
//Set up custom telemetry filtration
SetUpTelemetryProcessors();
//send the license information if it has not already been sent for this Middleware instance startup
SendLicenseConfiguration();
//Track the event
Client.TrackEvent("Telemetry Opt In", MonitoringSettings.GetAsDictionary());
_initialized = true;
}
}
值得一提的是,如果我將遙測密鑰添加到應用程式配置中,tracelistener 就可以作業......出于某種原因,當我們以編程方式添加它時,它缺少對具有正確檢測密鑰的原始遙測配置物件的參考,我在想這是因為我正在使用 appinsights 的偵聽器創建一個新的 TraceSource 物件,其中包括一個新的配置實體。
uj5u.com熱心網友回復:
謝謝佳瑤。發布您的建議作為答案以幫助其他社區成員。
使應用程式能夠跟蹤代碼的執行并將跟蹤訊息與其源相關聯的屬性。
public class TraceSource
下面的代碼可幫助您理解Tracesource類并演示開關和過濾器的用法。
// The following configuration file can be used with this sample.
// When using a configuration file #define ConfigFile.
// <source name="TraceTest" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch" >
// <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false" />
// <remove name ="Default" />
// <!-- You can set the level at which tracing is to occur -->
// <add name="SourceSwitch" value="Warning" />
// <!-- You can turn tracing off -->
// <!--add name="SourceSwitch" value="Off" -->
// <trace autoflush="true" indentsize="4"></trace>
#define TRACE
//#define ConfigFile
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Security.Permissions;
namespace Testing
{
class TraceTest
{
// Initialize the trace source.
static TraceSource ts = new TraceSource("TraceTest");
[SwitchAttribute("SourceSwitch", typeof(SourceSwitch))]
static void Main()
{
try
{
// Initialize trace switches.
#if(!ConfigFile)
SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");
ts.Switch = sourceSwitch;
int idxConsole = ts.Listeners.Add(new System.Diagnostics.ConsoleTraceListener());
ts.Listeners[idxConsole].Name = "console";
#endif
DisplayProperties(ts);
ts.Listeners["console"].TraceOutputOptions |= TraceOptions.Callstack;
ts.TraceEvent(TraceEventType.Warning, 1);
ts.Listeners["console"].TraceOutputOptions = TraceOptions.DateTime;
// Issue file not found message as a warning.
ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");
// Issue file not found message as a verbose event using a formatted string.
ts.TraceEvent(TraceEventType.Verbose, 3, "File {0} not found.", "test");
// Issue file not found message as information.
ts.TraceInformation("File {0} not found.", "test");
ts.Listeners["console"].TraceOutputOptions |= TraceOptions.LogicalOperationStack;
// Issue file not found message as an error event.
ts.TraceEvent(TraceEventType.Error, 4, "File {0} not found.", "test");
// Test the filter on the ConsoleTraceListener.
ts.Listeners["console"].Filter = new SourceFilter("No match");
ts.TraceData(TraceEventType.Error, 5,
"SourceFilter should reject this message for the console trace listener.");
ts.Listeners["console"].Filter = new SourceFilter("TraceTest");
ts.TraceData(TraceEventType.Error, 6,
"SourceFilter should let this message through on the console trace listener.");
ts.Listeners["console"].Filter = null;
// Use the TraceData method.
ts.TraceData(TraceEventType.Warning, 7, new object());
ts.TraceData(TraceEventType.Warning, 8, new object[] { "Message 1", "Message 2" });
// Activity tests.
ts.TraceEvent(TraceEventType.Start, 9, "Will not appear until the switch is changed.");
ts.Switch.Level = SourceLevels.ActivityTracing | SourceLevels.Critical;
ts.TraceEvent(TraceEventType.Suspend, 10, "Switch includes ActivityTracing, this should appear");
ts.TraceEvent(TraceEventType.Critical, 11, "Switch includes Critical, this should appear");
ts.Flush();
ts.Close();
Console.WriteLine("Press any key to exit.");
Console.Read();
}
catch (Exception e)
{
// Catch any unexpected exception.
Console.WriteLine("Unexpected exception: " e.ToString());
Console.Read();
}
}
public static void DisplayProperties(TraceSource ts)
{
Console.WriteLine("TraceSource name = " ts.Name);
Console.WriteLine("TraceSource switch level = " ts.Switch.Level);
Console.WriteLine("TraceSource switch = " ts.Switch.DisplayName);
SwitchAttribute[] switches = SwitchAttribute.GetAll(typeof(TraceTest).Assembly);
for (int i = 0; i < switches.Length; i )
{
Console.WriteLine("Switch name = " switches[i].SwitchName);
Console.WriteLine("Switch type = " switches[i].SwitchType);
}
#if(ConfigFile)
// Get the custom attributes for the TraceSource.
Console.WriteLine("Number of custom trace source attributes = "
ts.Attributes.Count);
foreach (DictionaryEntry de in ts.Attributes)
Console.WriteLine("Custom trace source attribute = "
de.Key " " de.Value);
// Get the custom attributes for the trace source switch.
foreach (DictionaryEntry de in ts.Switch.Attributes)
Console.WriteLine("Custom switch attribute = "
de.Key " " de.Value);
#endif
Console.WriteLine("Number of listeners = " ts.Listeners.Count);
foreach (TraceListener traceListener in ts.Listeners)
{
Console.Write("TraceListener: " traceListener.Name "\t");
// The following output can be used to update the configuration file.
Console.WriteLine("AssemblyQualifiedName = "
(traceListener.GetType().AssemblyQualifiedName));
}
}
}
}
有關更多資訊,請驗證 TraceSource 類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338130.html
標籤:asp.net 天蓝色 周转 azure-application-insights 痕迹
上一篇:為什么Pakku在MacOSBigSur上使用時會因“無法找到本機庫libarchive.13.dylib”而崩潰
