解决electron-wix-msi创建MSI时错误日志函数报错及崩溃日志存储问题
问题修复与崩溃日志实现
一、修复MSI生成错误
出现TypeError: msiCreator.defineMsiErrorLogFunction is not a function的核心原因是electron-wix-msi库的MSICreator类不存在该方法,属于无效自定义调用。修复步骤如下:
移除无效代码段
在msi_creator.js中,删除从// Step 5: Define the MSIErrorLog DLL function到console.log("Defined the MSIErrorLog DLL function.");的所有代码。清理冗余依赖
child_process、fs是Node.js内置模块,无需单独安装;其余几个依赖也并非生成MSI的必需项,执行以下命令卸载:npm uninstall node-gyp child_process fs exe-icon-extractor修正后的
msi_creator.js代码const { MSICreator } = require("electron-wix-msi"); const path = require("path"); async function start() { try { const msiCreator = new MSICreator({ appDirectory: path.resolve("."), description: "ABC", exe: "ABC.exe", name: "ABC", manufacturer: "FFFF", version: "0.1.0", upgradeCode: "{12343238-1111-2222-3333-123483275556}", removeExistingProduct: true, iconPath: "./abc.ico", outputDirectory: path.resolve("./output/"), }); console.log("Creating the .wxs template file..."); await msiCreator.create(); console.log("Created the .wxs template file."); console.log("Compiling the .wxs template..."); await msiCreator.compile(); console.log("Compiled the .wxs template to an MSI installer."); } catch (error) { console.error(error); } } start();重新运行
node .\msi_creator.js即可正常生成MSI包。
二、实现应用崩溃日志捕获
MSI是安装工具,无法直接捕获应用运行时的崩溃日志,需在Go程序内部实现panic捕获与日志写入:
修改
main.go代码
添加recover()捕获panic,将崩溃信息写入系统临时目录:package main import ( "log" "os" "path/filepath" "runtime/debug" ) func main() { // 延迟执行panic捕获逻辑 defer func() { if err := recover(); err != nil { // 获取系统临时目录路径 tempDir := os.TempDir() logPath := filepath.Join(tempDir, "abc_crash.log") // 创建/打开日志文件 logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { return } defer logFile.Close() // 写入崩溃详情与堆栈跟踪 logger := log.New(logFile, "CRASH: ", log.Ldate|log.Ltime) logger.Printf("Panic details: %v\nStack trace:\n%s", err, debug.Stack()) } }() // 触发除零错误 x := 0 _ = 1 / x }重新编译Go程序
$Env:GOOS="windows"; go build -ldflags "-H windowsgui" -o ABC.exe重新生成MSI
再次执行node .\msi_creator.js,安装并运行程序后,崩溃日志会写入系统临时目录(如C:\Users\<用户名>\AppData\Local\Temp\abc_crash.log),包含崩溃信息与完整堆栈跟踪。
内容的提问来源于stack exchange,提问作者manoos
相关产品推荐
相关产品推荐

