共用方式為


在語音 SDK 中啟用記錄

「記錄至檔案」是語音 SDK 的選用功能。 在開發期間,記錄功能會從語音 SDK 的核心元件提供其他資訊和診斷。 您可以將語音組態物件上的 屬性設定 Speech_LogFilename 為記錄檔的位置和名稱,以啟用它。 記錄是由語音 SDK 原生連結庫中的靜態類別處理。 您可以開啟任何語音 SDK 辨識器或合成器實例的記錄。 相同進程中的所有實例都會將記錄專案寫入相同的記錄檔。

範例

記錄檔名稱是在組態物件上指定。 SpeechConfig以 作為範例,並假設您已建立名為 speechConfig的實例:

speechConfig.SetProperty(PropertyId.Speech_LogFilename, "LogfilePathAndName");
speechConfig.setProperty(PropertyId.Speech_LogFilename, "LogfilePathAndName");
speechConfig->SetProperty(PropertyId::Speech_LogFilename, "LogfilePathAndName");
speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LogfilePathAndName")
[speechConfig setPropertyTo:@"LogfilePathAndName" byId:SPXSpeechLogFilename];
import ("github.com/Microsoft/cognitive-services-speech-sdk-go/common")

speechConfig.SetProperty(common.SpeechLogFilename, "LogfilePathAndName")

您可以從設定物件建立識別器。 這可啟用所有辨識器的記錄。

注意

如果您從設定物件建立 SpeechSynthesizer,則不會啟用記錄功能。 不過,如果啟用記錄功能,您也會從 SpeechSynthesizer收到診斷。

JavaScript 是透過 SDK 診斷啟用記錄的例外狀況,如下列程式碼片段所示:

sdk.Diagnostics.SetLoggingLevel(sdk.LogLevel.Debug);
sdk.Diagnostics.SetLogOutputPath("LogfilePathAndName");

在不同的平臺上建立記錄檔

針對 Windows 或 Linux,記錄檔可以位於使用者具有寫入許可權的任何路徑中。 其他作業系統中文件系統位置的寫入許可權預設可能會受到限制或限制。

通用 Windows 平台 (UWP)

UWP 應用程式需要將記錄檔放在其中一個應用程式資料位置(本機、漫遊或暫時性)。 您可以在本機應用程式資料夾中建立記錄檔:

StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile logFile = await storageFolder.CreateFileAsync("logfile.txt", CreationCollisionOption.ReplaceExisting);
speechConfig.SetProperty(PropertyId.Speech_LogFilename, logFile.Path);

在 Unity UWP 應用程式中,可以使用應用程式持續性數據路徑資料夾來建立記錄檔,如下所示:

#if ENABLE_WINMD_SUPPORT
    string logFile = Application.persistentDataPath + "/logFile.txt";
    speechConfig.SetProperty(PropertyId.Speech_LogFilename, logFile);
#endif

如需 UWP 應用程式中檔案存取權限的詳細資訊,請參閱 檔案訪問許可權

Android

您可以將記錄檔儲存到內部記憶體、外部記憶體或快取目錄。 在內部記憶體或快取目錄中建立的檔案是應用程式的私人檔案。 最好是在外部記憶體中建立記錄檔。

File dir = context.getExternalFilesDir(null);
File logFile = new File(dir, "logfile.txt");
speechConfig.setProperty(PropertyId.Speech_LogFilename, logFile.getAbsolutePath());

上述程式代碼會將記錄檔儲存至應用程式特定目錄根目錄中的外部記憶體。 用戶可以使用檔案管理員存取檔案(通常是 在 中 Android/data/ApplicationName/logfile.txt)。 卸載應用程式時,會刪除檔案。

您也需要要求 WRITE_EXTERNAL_STORAGE 指令清單檔案中的權限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
  ...
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  ...
</manifest>

在 Unity Android 應用程式中,可以使用應用程式持續性資料路徑資料夾來建立記錄檔,如下所示:

string logFile = Application.persistentDataPath + "/logFile.txt";
speechConfig.SetProperty(PropertyId.Speech_LogFilename, logFile);

此外,您也必須在 Android 的 Unity Player 設定中將寫入許可權設定為 “External (SDCard)”。 記錄會寫入至您可以使用 AndroidStudio Device 檔案總管 等工具取得的目錄。 確切的目錄路徑可能會因 Android 裝置而異。 位置通常是 sdcard/Android/data/your-app-packagename/files 目錄。

如需 Android 應用程式資料和檔案記憶體的詳細資訊,請參閱 這裡

iOS

只有應用程式沙箱內的目錄可供存取。 檔案可以在檔、文檔庫和暫存目錄中建立。 檔案目錄中的檔案可以提供給使用者使用。

如果您在 iOS 上使用 Objective-C,請使用下列代碼段在應用程式檔案目錄中建立記錄檔:

NSString *filePath = [
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    stringByAppendingPathComponent:@"logfile.txt"];
[speechConfig setPropertyTo:filePath byId:SPXSpeechLogFilename];

若要存取已建立的檔案,請將下列屬性新增至 Info.plist 應用程式的屬性清單:

<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

如果您在 iOS 上使用 Swift,請使用下列代碼段來啟用記錄:

let documentsDirectoryPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let documentsDirectoryPath = NSURL(string: documentsDirectoryPathString)!
let logFilePath = documentsDirectoryPath.appendingPathComponent("swift.log")
self.speechConfig!.setPropertyTo(logFilePath!.absoluteString, by: SPXPropertyId.speechLogFilename)

如需 iOS 檔系統的詳細資訊,請參閱 這裡

使用多個辨識器進行記錄

雖然記錄檔輸出路徑會指定為 或其他 SDK 物件的組態屬性 SpeechRecognizer ,但 SDK 記錄是單一且全進程的功能,沒有個別實例的概念。 您可以將此視為 SpeechRecognizer 建構函式(或類似的)隱含呼叫靜態和內部「設定全域記錄」例程,以及對應 SpeechConfig中可用的屬性數據。

這表示舉例來說您無法將六個平行識別器設定為同時輸出至六個不同的檔案。 相反地,建立的最新辨識器會將全域記錄實例設定為輸出至其組態屬性中指定的檔案,並將所有 SDK 記錄發出至該檔案。

這也表示設定記錄的物件存留期不會與記錄的持續時間相關聯。 記錄不會停止回應 SDK 對象的發行,而且只要未提供任何新的記錄設定,記錄就會繼續。 啟動之後,在建立新物件時,將記錄檔路徑設定為空字串,即可停止全進程記錄。

若要在設定多個實例的記錄時降低潛在的混淆,從執行實際工作的物件抽象控制記錄可能很有用。 協助程式例程的範例:

void EnableSpeechSdkLogging(const char* relativePath)
{
	auto configForLogging = SpeechConfig::FromSubscription("unused_key", "unused_region");
	configForLogging->SetProperty(PropertyId::Speech_LogFilename, relativePath);
	auto emptyAudioConfig = AudioConfig::FromStreamInput(AudioInputStream::CreatePushStream());
	auto temporaryRecognizer = SpeechRecognizer::FromConfig(configForLogging, emptyAudioConfig);
}

void DisableSpeechSdkLogging()
{
	EnableSpeechSdkLogging("");
}

下一步