{"record":{"id":"d6288a76a9843e65","repo":"hashicorp/nomad","slug":"failed-to-open-windows-eventlog-w","errorCode":null,"errorMessage":"Failed to open Windows eventlog: %w","messagePattern":"Failed to open Windows eventlog: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/winsvc/event_logger_windows.go","lineNumber":17,"sourceCode":"// Copyright IBM Corp. 2015, 2026\n// SPDX-License-Identifier: BUSL-1.1\n\npackage winsvc\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\n\t\"golang.org/x/sys/windows/svc/eventlog\"\n)\n\n// NewEventLogger creates a new event logger instance\nfunc NewEventLogger(level string) (io.WriteCloser, error) {\n\tevtLog, err := eventlog.Open(WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to open Windows eventlog: %w\", err)\n\t}\n\n\treturn &eventLogger{\n\t\tevtLog: evtLog,\n\t\tlevel:  EventlogLevelFromString(level),\n\t}, nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/winsvc/event_logger_windows.go#L1-L25","documentation":"NewEventLogger opens the Windows event log source named WINDOWS_SERVICE_NAME via the eventlog package; if Open fails (source not registered or insufficient privileges) the error is wrapped and returned, so no event logger can be created.","triggerScenarios":"Calling winsvc.NewEventLogger(level) on Windows when the event source has not been registered with the registry (no RegisterEventSource entry) or the process lacks permission to open it.","commonSituations":"Running the service binary without prior event-source registration (missing `eventlog.InstallAsEventCreate` step); running as a non-privileged user; accidentally invoking windows-only code on a non-Windows build where the API is unavailable anyway.","solutions":["Register the event source once at install time (eventlog.InstallAsEventCreate with WINDOWS_SERVICE_NAME)","Run the service under an account with permission to open the event log","Run on Windows only; guard windows-only logging paths behind runtime/build tags"],"exampleFix":"// before\nw, err := winsvc.NewEventLogger(\"INFO\") // source not registered\n// after\nif err := eventlog.InstallAsEventCreate(winsvc.WINDOWS_SERVICE_NAME, eventlog.Error|eventlog.Warning|eventlog.Info); err != nil && !strings.Contains(err.Error(), \"already exists\") {\n\tlog.Fatal(err)\n}\nw, err := winsvc.NewEventLogger(\"INFO\")","handlingStrategy":"try-catch","validationCode":"if runtime.GOOS != \"windows\" {\n\treturn errors.New(\"eventlog only available on Windows\")\n}\nif _, err := eventlog.Open(winsvc.WINDOWS_SERVICE_NAME); err != nil {\n\t// register the source before creating the logger\n\tif cerr := eventlog.InstallAsEventCreate(winsvc.WINDOWS_SERVICE_NAME, eventlog.Error|eventlog.Warning|eventlog.Info); cerr != nil && !strings.Contains(cerr.Error(), \"already exists\") {\n\t\treturn cerr\n\t}\n}","typeGuard":null,"tryCatchPattern":"w, err := winsvc.NewEventLogger(level)\nif err != nil {\n\tif strings.Contains(err.Error(), \"Failed to open Windows eventlog\") {\n\t\t// fall back to file/stderr logging and register the source for next start\n\t\treturn ioutil.Discard, nil // or a file logger\n\t}\n\treturn nil, err\n}","preventionTips":["Register the event source during installation (eventlog.InstallAsEventCreate)","Run the service under an account with event-log access rights","Keep windows-only code behind build tags so it never runs on other platforms","Implement a file/stdout logging fallback when the eventlog is unavailable"],"tags":["windows","eventlog","service","logging"],"backgroundTag":"windows-eventlog-open-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}