@@ -35,6 +35,8 @@ public static ActionResult SaveDeferredCustomActionData(Session session)
35
35
var customActionData = new CustomActionData
36
36
{
37
37
{ "NR_LICENSE_KEY" , session [ "NR_LICENSE_KEY" ] } ,
38
+ { "NR_APP_NAME" , session [ "NR_APP_NAME" ] } ,
39
+ { "NR_USE_APP_POOL_NAMING" , session [ "NR_USE_APP_POOL_NAMING" ] } ,
38
40
{ "NETAGENTCOMMONFOLDER" , session [ "NETAGENTCOMMONFOLDER" ] } ,
39
41
{ "LOGSFOLDER" , session [ "LOGSFOLDER" ] } ,
40
42
{ "NETAGENTFOLDER" , session [ "NETAGENTFOLDER" ] } ,
@@ -43,6 +45,7 @@ public static ActionResult SaveDeferredCustomActionData(Session session)
43
45
44
46
session . DoAction ( "MigrateConfiguration" , customActionData ) ;
45
47
session . DoAction ( "SetLicenseKey" , customActionData ) ;
48
+ session . DoAction ( "SetAppName" , customActionData ) ;
46
49
session . DoAction ( "CleanupPreviousInstall" , customActionData ) ;
47
50
48
51
return ActionResult . Success ;
@@ -111,6 +114,84 @@ public static ActionResult SetLicenseKey(Session session)
111
114
return ActionResult . Success ;
112
115
}
113
116
117
+ [ CustomAction ]
118
+ public static ActionResult SetAppName ( Session session )
119
+ {
120
+ try
121
+ {
122
+ var path = session . CustomActionData [ "NETAGENTCOMMONFOLDER" ] + @"\newrelic.config" ;
123
+
124
+ // appName will be an empty string if the property wasn't set via the command line,
125
+ // OR if the property was set with an empty string (NR_APP_NAME="")
126
+ var appName = session . CustomActionData [ "NR_APP_NAME" ] ;
127
+ var useAppPoolNaming = session . CustomActionData [ "NR_USE_APP_POOL_NAMING" ] . Equals ( bool . TrueString , StringComparison . CurrentCultureIgnoreCase )
128
+ || session . CustomActionData [ "NR_USE_APP_POOL_NAMING" ] . Equals ( "1" ) ;
129
+
130
+ session . Log ( $ "SetAppName: appName={ appName } , useAppPoolNaming={ useAppPoolNaming } ") ;
131
+ if ( string . IsNullOrEmpty ( appName ) && ! useAppPoolNaming )
132
+ {
133
+ session . Log ( "NR_APP_NAME was not set or was set to an empty string, and NR_USE_APP_POOL_NAMING was not set or was not set to true. Application name not modified." ) ;
134
+ return ActionResult . Success ;
135
+ }
136
+
137
+ var document = new XmlDocument ( ) ;
138
+ document . Load ( path ) ;
139
+ var namespaceManager = new XmlNamespaceManager ( document . NameTable ) ;
140
+ namespaceManager . AddNamespace ( "newrelic-config" , "urn:newrelic-config" ) ;
141
+
142
+ var applicationNode = document . SelectSingleNode ( "/newrelic-config:configuration/newrelic-config:application" , namespaceManager ) ;
143
+ if ( applicationNode == null )
144
+ {
145
+ session . Log ( "Unable to locate /configuration/application node in newrelic.config. Application name not set." ) ;
146
+ return ActionResult . Success ;
147
+ }
148
+
149
+ // NR_USE_APP_POOL_NAMING takes precedence over anything set in NR_APP_NAME
150
+ if ( useAppPoolNaming )
151
+ {
152
+ session . Log ( "NR_APP_NAME = UseAppPoolNaming, deleting name nodes from application element" ) ;
153
+ var childNodes = applicationNode . ChildNodes ;
154
+ foreach ( XmlNode childNode in childNodes )
155
+ {
156
+ session . Log ( $ "found child node with name { childNode . Name } ") ;
157
+ if ( childNode . Name . Equals ( "name" ) )
158
+ {
159
+ session . Log ( "Deleting 'name' node" ) ;
160
+ applicationNode . RemoveChild ( childNode ) ;
161
+ }
162
+ }
163
+ document . Save ( session . CustomActionData [ "NETAGENTCOMMONFOLDER" ] + @"\newrelic.config" ) ;
164
+ session . Log ( "newrelic.config saved with application name elements removed." ) ;
165
+ return ActionResult . Success ;
166
+ }
167
+
168
+ // If we get here, NR_USE_APP_POOL_NAMING was not set or not set to true,
169
+ // and NR_APP_NAME has a non-empty value. Set the app name to NR_APP_NAME.
170
+ // If no <name>appName</name> node exists, create it
171
+ XmlNode applicationNameNode ;
172
+ applicationNameNode = document . SelectSingleNode ( "/newrelic-config:configuration/newrelic-config:application/newrelic-config:name" , namespaceManager ) ;
173
+ if ( applicationNameNode == null )
174
+ {
175
+ session . Log ( "Unable to locate existing /configuration/application/name node in newrelic.config, creating a new one." ) ;
176
+ var newNameNode = document . CreateElement ( "name" , "urn:newrelic-config" ) ;
177
+ applicationNameNode = applicationNode . AppendChild ( newNameNode ) ;
178
+ }
179
+ session . Log ( "/configuration/application/name node found or created in newrelic.config" ) ;
180
+
181
+ applicationNameNode . InnerText = appName ;
182
+ session . Log ( "Application name set to " + appName ) ;
183
+
184
+ document . Save ( session . CustomActionData [ "NETAGENTCOMMONFOLDER" ] + @"\newrelic.config" ) ;
185
+ session . Log ( "newrelic.config saved with updated application name." ) ;
186
+ }
187
+ catch ( Exception exception )
188
+ {
189
+ session . Log ( "Exception while attempting to set the application name in newrelic.config.\n {0}" , exception ) ;
190
+ }
191
+
192
+ return ActionResult . Success ;
193
+ }
194
+
114
195
[ CustomAction ]
115
196
public static ActionResult CleanupPreviousInstall ( Session session )
116
197
{
0 commit comments