監控、偵測、診斷與故障排除,使服務能在對使用者體驗的干擾最小的情況下持續運作。 監控與診斷在實際部署的生產環境中至關重要。 在服務開發時採用類似模式,確保診斷流程在你轉到生產環境時依然正常運作。 Service Fabric 讓服務開發者能輕鬆實作診斷,並能無縫跨越單機本地開發與實際生產叢集架構。
偵錯 Service Fabric Java 應用程式
對於 Java 應用程式,有 多種日誌框架 可供選擇。 由於 java.util.logging 是 JRE 的預設選項,因此 GitHub 的程式碼範例也會用它。 以下討論將說明如何配置框架 java.util.logging 。
使用 java.util.logging ,你可以將應用程式日誌重新導向到記憶體、輸出串流、主控台檔案或套接字。 對於這些選項,框架中已預設有處理程序。 你可以建立一個 app.properties 檔案來設定應用程式的檔案處理器,將所有日誌重新導向到本地檔案。
以下程式碼片段包含一個範例配置:
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.level = ALL
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.limit = 1024000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.pattern = /tmp/servicefabric/logs/mysfapp%u.%g.log
檔案指向的資料夾 app.properties 必須存在。 建立 app.properties 檔案後,您還需要修改位於 <applicationfolder>/<servicePkg>/Code/ 資料夾中的進入點指令碼 entrypoint.sh,將屬性 java.util.logging.config.file 設定為 app.properties 檔案。 條目應該如下範例:
java -Djava.library.path=$LD_LIBRARY_PATH -Djava.util.logging.config.file=<path to app.properties> -jar <service name>.jar
此組態會讓系統以輪替方式在 /tmp/servicefabric/logs/ 收集記錄。 在此情況下,日誌檔案名為 mysfapp%u.%g.log,其中:
- %u 是一個唯一數值,用來解決同時進行的 Java 程序間的衝突。
- %g 是用來區分旋轉日誌的版本號。
根據預設,如果未明確設定任何處理常式,就會註冊主控台處理常式。 你可以在 syslog 的 /var/log/syslog 下查看日誌。
更多資訊請參閱 GitHub 上的程式碼範例。
除錯 Service Fabric C# 應用程式
Linux 上有多種框架可用於追蹤 CoreCLR 應用程式。 更多資訊請參閱 .NET 日誌擴充功能。 由於 C# 開發者對 EventSource 很熟悉,本文在 Linux 的 CoreCLR 範例中使用 EventSource 進行追蹤。
第一步是加入 System.Diagnostics.Tracing 功能,讓你能將日誌寫入記憶體、輸出串流或主控台檔案。 若要使用 EventSource 進行日誌記錄,請將以下專案加入您的 project.json:
"System.Diagnostics.StackTrace": "4.0.1"
你可以使用自訂的 EventListener 來監聽服務事件,然後適當地將它們導向到追蹤檔案。 以下程式碼片段展示了使用 EventSource 與自訂 EventListener 進行日誌記錄的範例實作:
public class ServiceEventSource : EventSource
{
public static ServiceEventSource Current = new ServiceEventSource();
[NonEvent]
public void Message(string message, params object[] args)
{
if (this.IsEnabled())
{
var finalMessage = string.Format(message, args);
this.Message(finalMessage);
}
}
// TBD: Need to add method for sample event.
}
internal class ServiceEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
using (StreamWriter Out = new StreamWriter( new FileStream("/tmp/MyServiceLog.txt", FileMode.Append)))
{
// report all event information
Out.Write(" {0} ", Write(eventData.Task.ToString(), eventData.EventName, eventData.EventId.ToString(), eventData.Level,""));
if (eventData.Message != null)
Out.WriteLine(eventData.Message, eventData.Payload.ToArray());
else
{
string[] sargs = eventData.Payload != null ? eventData.Payload.Select(o => o.ToString()).ToArray() : null;
Out.WriteLine("({0}).", sargs != null ? string.Join(", ", sargs) : "");
}
}
}
}
前一個片段會將日誌輸出到/tmp/MyServiceLog.txt的檔案中。 這個檔名需要適當更新。 如果你想將日誌重新導向到主控台,請在自訂的 EventListener 類別中使用以下片段:
public static TextWriter Out = Console.Out;
C# Samples 的取樣會使用 EventSource 和自訂的 EventListener 來將事件記錄到檔案中。
下一步
你應用程式中加入的同一套追蹤程式碼,也能用於 Azure 叢集上的應用程式診斷。 請參考這些文章,討論各種工具的選項,並說明如何設定它們。