1

我们正在尝试通过从 java 执行以下命令来创建 Windows 事件日志。但无法在描述(/d)中添加换行符(\n)。

eventcreate /t error /id 100 /l myApp /SO "mysource" /d "这是日志的第一行 \n 这是第二行"

从java代码执行上面: Runtime.getRuntime().exec(command);

1.尝试添加 crl+l (^L) 而不是 \n 但它仅适用于命令行而不适用于 java 代码。

4

1 回答 1

1

终于得到了解决方案:

我们可以传递字符串数组,而不是将字符串作为参数传递给 .exec() 方法。这样,日志按摩将 \n 识别为新行。下面的代码片段现在可以工作了。

String[] command = {"eventcreate", "/t", "error", "/id","100","/l", "CustomApp", "/SO","CustomSource","/d","this is 1st line of the log \n this is second line"};       
Runtime.getRuntime().exec(command);
于 2015-10-20T03:13:52.640 回答