在语音 SDK 中启用日志记录Enable logging in the Speech SDK

将日志记录到文件是语音 SDK 的一项可选功能。Logging to file is an optional feature for the Speech SDK. 在开发期间,日志记录可以提供语音 SDK 核心组件的附加信息和诊断数据。During development logging provides additional information and diagnostics from the Speech SDK's core components. 将语音配置对象中的属性 Speech_LogFilename 设置为日志文件的位置和名称可以启用日志记录。It can be enabled by setting the property Speech_LogFilename on a speech configuration object to the location and name of the log file. 基于该配置创建识别器后,将会全局激活日志记录,以后无法将其禁用。Logging will be activated globally once a recognizer is created from that configuration and can't be disabled afterwards. 在运行日志记录会话期间,无法更改日志文件的名称。You can't change the name of a log file during a running logging session.

备注

从语音 SDK 版本 1.4.0 开始,日志记录可在所有支持的语音 SDK 编程语言中使用,但 JavaScript 除外。Logging is available since Speech SDK version 1.4.0 in all supported Speech SDK programming languages, with the exception of JavaScript.

示例Sample

日志文件名在配置对象中指定。The log file name is specified on a configuration object. SpeechConfig 为例,假设你已创建名为 config 的实例:Taking the SpeechConfig as an example and assuming that you have created an instance called config:

config.SetProperty(PropertyId.Speech_LogFilename, "LogfilePathAndName");
config.setProperty(PropertyId.Speech_LogFilename, "LogfilePathAndName");
config->SetProperty(PropertyId::Speech_LogFilename, "LogfilePathAndName");
config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LogfilePathAndName")
[config setPropertyTo:@"LogfilePathAndName" byId:SPXSpeechLogFilename];

可以基于配置对象创建识别器。You can create a recognizer from the config object. 这会为所有识别器启用日志记录。This will enable logging for all recognizers.

备注

如果基于配置对象创建 SpeechSynthesizer,则不会启用日志记录。If you create a SpeechSynthesizer from the config object, it will not enable logging. 不过,如果启用了日志记录,则还会从 SpeechSynthesizer 收到诊断数据。If logging is enabled though, you will also receive diagnostics from the SpeechSynthesizer.

在不同的平台上创建日志文件Create a log file on different platforms

对于 Windows 或 Linux,日志文件可以位于用户有权写入的任何路径。For Windows or Linux, the log file can be in any path the user has write permission for. 默认情况下,对其他操作系统中的文件系统位置的写入权限可能会受到限制。Write permissions to file system locations in other operating systems may be limited or restricted by default.

通用 Windows 平台 (UWP)Universal Windows Platform (UWP)

UWP 应用程序需将日志文件放在某个应用程序数据位置(本地、漫游或临时位置)。UWP applications need to be places log files in one of the application data locations (local, roaming, or temporary). 可在本地应用程序文件夹中创建日志文件:A log file can be created in the local application folder:

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

此处提供了有关 UWP 应用程序的文件访问权限的详细信息。More about file access permission for UWP applications is available here.

AndroidAndroid

可将日志文件保存到内部存储、外部存储或缓存目录。You can save a log file to either internal storage, external storage, or the cache directory. 在内部存储或缓存目录中创建的文件专用于应用程序。Files created in the internal storage or the cache directory are private to the application. 最好是在外部存储中创建日志文件。It is preferable to create a log file in external storage.

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

以上代码将日志文件保存到应用程序特定的目录的根目录中的外部存储。The code above will save a log file to the external storage in the root of an application-specific directory. 用户可以使用文件管理器访问该文件(通常位于 Android/data/ApplicationName/logfile.txt)。A user can access the file with the file manager (usually in Android/data/ApplicationName/logfile.txt). 卸载应用程序时,会删除该文件。The file will be deleted when the application is uninstalled.

还需要请求清单文件中的 WRITE_EXTERNAL_STORAGE 权限:You also need to request WRITE_EXTERNAL_STORAGE permission in the manifest file:

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

此处提供了有关适用于 Android 应用程序的数据和文件存储的详细信息。More about data and file storage for Android applications is available here.

iOSiOS

只能访问应用程序沙盒中的目录。Only directories inside the application sandbox are accessible. 可以在文档、库和临时目录中创建文件。Files can be created in the documents, library, and temp directories. 文档目录中的文件可供用户使用。Files in the documents directory can be made available to a user. 以下代码片段演示如何在应用程序文档目录中创建日志文件:The following code snippet shows creation of a log file in the application document directory:

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

若要访问创建的文件,请将以下属性添加到应用程序的 Info.plist 属性列表中:To access a created file, add the below properties to the Info.plist property list of the application:

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

此处提供了有关 iOS 文件系统的详细信息。More about iOS File System is available here.

后续步骤Next steps