1
- /*
2
- * ApplicationInsights-Java
3
- * Copyright (c) Microsoft Corporation
4
- * All rights reserved.
5
- *
6
- * MIT License
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
8
- * software and associated documentation files (the ""Software""), to deal in the Software
9
- * without restriction, including without limitation the rights to use, copy, modify, merge,
10
- * publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11
- * persons to whom the Software is furnished to do so, subject to the following conditions:
12
- * The above copyright notice and this permission notice shall be included in all copies or
13
- * substantial portions of the Software.
14
- * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
- * DEALINGS IN THE SOFTWARE.
20
- */
21
- package com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw ;
22
-
23
- import java .util .Map ;
24
-
25
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .ApplicationMetadataFactory ;
26
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .DiagnosticsHelper ;
27
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaError ;
28
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaInfo ;
29
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaVerbose ;
30
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaWarn ;
31
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .model .IpaEtwEventBase ;
32
- import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .status .StatusFile ;
33
-
34
- import org .slf4j .LoggerFactory ;
35
-
36
- import ch .qos .logback .classic .Level ;
37
- import ch .qos .logback .classic .spi .ILoggingEvent ;
38
- import ch .qos .logback .classic .spi .ThrowableProxyUtil ;
39
- import ch .qos .logback .core .AppenderBase ;
40
-
41
- public class EtwAppender extends AppenderBase <ILoggingEvent > {
42
- private final EtwProvider etwProvider ;
43
- private final IpaEtwEventBase proto ;
44
-
45
- public EtwAppender () {
46
- ApplicationMetadataFactory metadata = DiagnosticsHelper .getMetadataFactory ();
47
-
48
- proto = new IpaInfo ();
49
- proto .setAppName (metadata .getSiteName ().getValue ());
50
- proto .setExtensionVersion (metadata .getSdkVersion ().getValue ());
51
- proto .setSubscriptionId (metadata .getSubscriptionId ().getValue ());
52
-
53
- etwProvider = new EtwProvider (metadata .getSdkVersion ().getValue ());
54
- }
55
-
56
- @ Override
57
- public void start () {
58
- IpaVerbose event = new IpaVerbose (proto );
59
- event .setMessageFormat ("EtwProvider initialized sucessfully." );
60
- try {
61
- this .etwProvider .writeEvent (event );
62
- } catch (LinkageError | ApplicationInsightsEtwException e ) {
63
- final String message = "EtwProvider failed to initialize." ;
64
- LoggerFactory .getLogger (DiagnosticsHelper .DIAGNOSTICS_LOGGER_NAME ).error (message , e );
65
- addError (message , e );
66
-
67
- StatusFile .putValue ("EtwProviderInitialized" , "false" );
68
- StatusFile .putValue ("EtwProviderError" , e .getLocalizedMessage ());
69
- StatusFile .write ();
70
-
71
- return ; // appender fails to start
72
- }
73
-
74
- StatusFile .putValueAndWrite ("EtwProviderInitialized" , "true" );
75
- super .start ();
76
- }
77
-
78
- @ Override
79
- protected void append (ILoggingEvent logEvent ) {
80
- String logger = logEvent .getLoggerName ();
81
- if (logger != null && logger .startsWith ("com.microsoft.applicationinsights.agent.bootstrap.diagnostics.etw." )) {
82
- addWarn ("Skipping attempt to log to " +logger );
83
- return ;
84
- }
85
-
86
- Level level = logEvent .getLevel ();
87
- IpaEtwEventBase event ;
88
- // empty if no throwable
89
- switch (level .levelInt ) {
90
- case Level .ERROR_INT :
91
- IpaError error = new IpaError (proto );
92
- error .setStacktrace (ThrowableProxyUtil .asString (logEvent .getThrowableProxy ()));
93
- event = error ;
94
- break ;
95
- case Level .WARN_INT :
96
- IpaWarn warn = new IpaWarn (proto );
97
- warn .setStacktrace (ThrowableProxyUtil .asString (logEvent .getThrowableProxy ()));
98
- event = warn ;
99
- break ;
100
- case Level .INFO_INT :
101
- event = new IpaInfo (proto );
102
- break ;
103
- default :
104
- addWarn ("Unsupported log level: " + level .levelStr );
105
- return ;
106
- }
107
-
108
- Map <String , String > mdcPropertyMap = logEvent .getMDCPropertyMap ();
109
- if (!mdcPropertyMap .isEmpty ()) {
110
- String operation = mdcPropertyMap .get (DiagnosticsHelper .MDC_PROP_OPERATION );
111
- if (operation != null && !operation .isEmpty ()) {
112
- event .setOperation (operation );
113
- }
114
- }
115
- event .setLogger (logger );
116
- event .setMessageFormat (logEvent .getMessage ());
117
- event .setMessageArgs (logEvent .getArgumentArray ());
118
- try {
119
- etwProvider .writeEvent (event );
120
- } catch (ApplicationInsightsEtwException e ) {
121
- addError ("Exception from EtwProvider: " + e .getLocalizedMessage (), e );
122
- }
123
- }
124
-
1
+ /*
2
+ * ApplicationInsights-Java
3
+ * Copyright (c) Microsoft Corporation
4
+ * All rights reserved.
5
+ *
6
+ * MIT License
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
8
+ * software and associated documentation files (the ""Software""), to deal in the Software
9
+ * without restriction, including without limitation the rights to use, copy, modify, merge,
10
+ * publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11
+ * persons to whom the Software is furnished to do so, subject to the following conditions:
12
+ * The above copyright notice and this permission notice shall be included in all copies or
13
+ * substantial portions of the Software.
14
+ * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ * DEALINGS IN THE SOFTWARE.
20
+ */
21
+ package com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw ;
22
+
23
+ import java .util .Map ;
24
+
25
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .ApplicationMetadataFactory ;
26
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .DiagnosticsHelper ;
27
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaError ;
28
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaInfo ;
29
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaVerbose ;
30
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .IpaWarn ;
31
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .etw .events .model .IpaEtwEventBase ;
32
+ import com .microsoft .applicationinsights .agent .bootstrap .diagnostics .status .StatusFile ;
33
+
34
+ import org .slf4j .LoggerFactory ;
35
+
36
+ import ch .qos .logback .classic .Level ;
37
+ import ch .qos .logback .classic .spi .ILoggingEvent ;
38
+ import ch .qos .logback .classic .spi .ThrowableProxyUtil ;
39
+ import ch .qos .logback .core .AppenderBase ;
40
+
41
+ public class EtwAppender extends AppenderBase <ILoggingEvent > {
42
+ private final EtwProvider etwProvider ;
43
+ private final IpaEtwEventBase proto ;
44
+
45
+ public EtwAppender () {
46
+ ApplicationMetadataFactory metadata = DiagnosticsHelper .getMetadataFactory ();
47
+
48
+ proto = new IpaInfo ();
49
+ proto .setAppName (metadata .getSiteName ().getValue ());
50
+ proto .setExtensionVersion (metadata .getSdkVersion ().getValue ());
51
+ proto .setSubscriptionId (metadata .getSubscriptionId ().getValue ());
52
+
53
+ etwProvider = new EtwProvider (metadata .getSdkVersion ().getValue ());
54
+ }
55
+
56
+ @ Override
57
+ public void start () {
58
+ IpaVerbose event = new IpaVerbose (proto );
59
+ event .setMessageFormat ("EtwProvider initialized sucessfully." );
60
+ try {
61
+ this .etwProvider .writeEvent (event );
62
+ } catch (LinkageError | ApplicationInsightsEtwException e ) {
63
+ final String message = "EtwProvider failed to initialize." ;
64
+ LoggerFactory .getLogger (DiagnosticsHelper .DIAGNOSTICS_LOGGER_NAME ).error (message , e );
65
+ addError (message , e );
66
+
67
+ StatusFile .putValue ("EtwProviderInitialized" , "false" );
68
+ StatusFile .putValue ("EtwProviderError" , e .getLocalizedMessage ());
69
+ StatusFile .write ();
70
+
71
+ return ; // appender fails to start
72
+ }
73
+
74
+ StatusFile .putValueAndWrite ("EtwProviderInitialized" , "true" );
75
+ super .start ();
76
+ }
77
+
78
+ @ Override
79
+ protected void append (ILoggingEvent logEvent ) {
80
+ String logger = logEvent .getLoggerName ();
81
+ if (logger != null && logger .startsWith ("com.microsoft.applicationinsights.agent.bootstrap.diagnostics.etw." )) {
82
+ addWarn ("Skipping attempt to log to " +logger );
83
+ return ;
84
+ }
85
+
86
+ Level level = logEvent .getLevel ();
87
+ IpaEtwEventBase event ;
88
+ // empty if no throwable
89
+ switch (level .levelInt ) {
90
+ case Level .ERROR_INT :
91
+ IpaError error = new IpaError (proto );
92
+ error .setStacktrace (ThrowableProxyUtil .asString (logEvent .getThrowableProxy ()));
93
+ event = error ;
94
+ break ;
95
+ case Level .WARN_INT :
96
+ IpaWarn warn = new IpaWarn (proto );
97
+ warn .setStacktrace (ThrowableProxyUtil .asString (logEvent .getThrowableProxy ()));
98
+ event = warn ;
99
+ break ;
100
+ case Level .INFO_INT :
101
+ event = new IpaInfo (proto );
102
+ break ;
103
+ default :
104
+ addWarn ("Unsupported log level: " + level .levelStr );
105
+ return ;
106
+ }
107
+
108
+ Map <String , String > mdcPropertyMap = logEvent .getMDCPropertyMap ();
109
+ if (!mdcPropertyMap .isEmpty ()) {
110
+ String operation = mdcPropertyMap .get (DiagnosticsHelper .MDC_PROP_OPERATION );
111
+ if (operation != null && !operation .isEmpty ()) {
112
+ event .setOperation (operation );
113
+ }
114
+ }
115
+ event .setLogger (logger );
116
+ event .setMessageFormat (logEvent .getMessage ());
117
+ event .setMessageArgs (logEvent .getArgumentArray ());
118
+ try {
119
+ etwProvider .writeEvent (event );
120
+ } catch (ApplicationInsightsEtwException e ) {
121
+ addError ("Exception from EtwProvider: " + e .getLocalizedMessage (), e );
122
+ }
123
+ }
124
+
125
125
}
0 commit comments