Change from reflection to type switch in Logrus hook

This commit is contained in:
Kevin Parsons
2018-12-13 17:23:55 -08:00
parent 62f7a6b7b9
commit cd4b2fefe9
2 changed files with 5 additions and 4 deletions
+2 -1
View File
@@ -8,7 +8,8 @@ import (
// InType indicates the type of data contained in the ETW event. // InType indicates the type of data contained in the ETW event.
type InType byte type InType byte
// Various InType definitions for TraceLogging. // Various InType definitions for TraceLogging. These must match the definitions
// found in TraceLoggingProvider.h in the Windows SDK.
const ( const (
InTypeNull InType = iota InTypeNull InType = iota
InTypeUnicodeString InTypeUnicodeString
+3 -3
View File
@@ -57,10 +57,10 @@ func (h *Hook) Fire(e *logrus.Entry) error {
event.Data.AddString(e.Message) event.Data.AddString(e.Message)
for k, v := range e.Data { for k, v := range e.Data {
switch reflect.TypeOf(v).Kind() { switch v := v.(type) {
case reflect.String: case string:
event.Metadata.AddField(k, etw.InTypeAnsiString) event.Metadata.AddField(k, etw.InTypeAnsiString)
event.Data.AddString(v.(string)) event.Data.AddString(v)
default: default:
event.Metadata.AddField(k, etw.InTypeAnsiString) event.Metadata.AddField(k, etw.InTypeAnsiString)
event.Data.AddString(fmt.Sprintf("<unknown type: %v> %v", reflect.TypeOf(v), v)) event.Data.AddString(fmt.Sprintf("<unknown type: %v> %v", reflect.TypeOf(v), v))