![]() |
VOOZH | about |
dotnet add package NewRelic.Xamarin.Plugin --version 1.0.1
NuGet\Install-Package NewRelic.Xamarin.Plugin -Version 1.0.1
<PackageReference Include="NewRelic.Xamarin.Plugin" Version="1.0.1" />
<PackageVersion Include="NewRelic.Xamarin.Plugin" Version="1.0.1" />Directory.Packages.props
<PackageReference Include="NewRelic.Xamarin.Plugin" />Project file
paket add NewRelic.Xamarin.Plugin --version 1.0.1
#r "nuget: NewRelic.Xamarin.Plugin, 1.0.1"
#:package NewRelic.Xamarin.Plugin@1.0.1
#addin nuget:?package=NewRelic.Xamarin.Plugin&version=1.0.1Install as a Cake Addin
#tool nuget:?package=NewRelic.Xamarin.Plugin&version=1.0.1Install as a Cake Tool
<a href="https://opensource.newrelic.com/oss-category/#community-plus"><picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/dark/Community_Plus.png"><source media="(prefers-color-scheme: light)" srcset="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/Community_Plus.png"><img alt="New Relic Open Source community plus project banner." src="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/Community_Plus.png"></picture></a>
This plugin allows you to instrument Xamarin Forms, iOS and Android apps with help of native New Relic Android and iOS Bindings. The New Relic SDKs collect crashes, network traffic, and other information for hybrid apps using native components.
Install NewRelic plugin into your Xamarin project by adding as NuGet Package NewRelic.Xamarin.Plugin.
Open your solution, select the project you want to add NewRelic package to and open its context menu. Unfold "Add" and click "Add NuGet packages...".
On your iOS app, you will also need to add NuGet package NewRelic.Xamarin.iOS.Binding to the dependencies.
App.xaml.cs and add the following code to launch NewRelic Plugin (don't forget to put proper application tokens):using NewRelic.Xamarin.Plugin;
...
public App ()
{
InitializeComponent();
MainPage = new MainPage();
Application.Current.PageAppearing += OnPageAppearing;
Application.Current.PageDisappearing += PageDisappearing;
CrossNewRelicClient.Current.HandleUncaughtException();
CrossNewRelicClient.Current.TrackShellNavigatedEvents()
// Set optional agent configuration
// Options are: crashReportingEnabled, loggingEnabled, logLevel, collectorAddress, crashCollectorAddress,analyticsEventEnabled, networkErrorRequestEnabled, networkRequestEnabled, interactionTracingEnabled,webViewInstrumentation, fedRampEnabled,offlineStorageEnabled,newEventSystemEnabled,backgroundReportingEnabled
// AgentStartConfiguration agentConfig = new AgentStartConfiguration(crashReportingEnabled:false);
if (Device.RuntimePlatform == Device.Android)
{
CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE>");
// Start with optional agent configuration
// CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE", agentConfig);
} else if (Device.RuntimePlatform == Device.iOS)
{
CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE>");
// Start with optional agent configuration
// CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE", agentConfig);
}
}
Properties/AndroidManifest.xml for your Android App and add the following permissions: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
The Xamarin mobile plugin allows you to track navigation events within the .NET MAUI Shell. In order to do so, you only need to call:
CrossNewRelicClient.Current.TrackShellNavigatedEvents();
It is recommended to call this method along when starting the agent. These events will only be recorded after navigation is complete. You can find this data through the data explorer in MobileBreadcrumb under the name ShellNavigated or by query:
SELECT * FROM MobileBreadcrumb WHERE name = 'ShellNavigated' SINCE 24 HOURS AGO
The breadcrumb will contain three attributes:
Current: The URI of the current page.Source: The type of navigation that occurred.Previous: The URI of the previous page. Will not exist if previous page was null.See the examples below, and for more detail, see New Relic iOS SDK doc or Android SDK .
Throws a demo run-time exception on Android/iOS to test New Relic crash reporting.
CrossNewRelicClient.Current.CrashNow();
Returns ID for the current session.
string sessionId = CrossNewRelicClient.Current.CurrentSessionId();
Track a method as an interaction.
End an interaction (Required). This uses the string ID for the interaction you want to end. This string is returned when you use startInteraction().
HttpClient myClient = new HttpClient(CrossNewRelicClient.Current.GetHttpMessageHandler());
string interactionId = CrossNewRelicClient.Current.StartInteraction("Getting data from service");
var response = await myClient.GetAsync(new Uri("https://jsonplaceholder.typicode.com/todos/1"));
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
} else
{
Console.WriteLine("Unsuccessful response code");
}
CrossNewRelicClient.Current.EndInteraction(interactionId);
Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.
CrossNewRelicClient.Current.NoticeHttpTransaction(
"https://newrelic.com",
"GET",
200,
DateTimeOffset.Now.ToUnixTimeMilliseconds(),
DateTimeOffset.Now.ToUnixTimeMilliseconds() + 100,
0,
1000,
""
);
Records network failures. If a network request fails, use this method to record details about the failure.
CrossNewRelicClient.Current.NoticeNetworkFailure(
"https://fakewebsite.com",
"GET",
DateTimeOffset.Now.ToUnixTimeMilliseconds(),
DateTimeOffset.Now.ToUnixTimeMilliseconds() + 100,
NetworkFailure.Unknown
);
This call creates and records a MobileBreadcrumb event, which can be queried with NRQL and in the crash event trail.
CrossNewRelicClient.Current.RecordBreadcrumb("XamarinExampleBreadcrumb", new Dictionary<string, object>()
{
{"BreadNumValue", 12.3 },
{"BreadStrValue", "XamBread" },
{"BreadBoolValue", true }
}
);
Creates and records a custom event for use in New Relic Insights.
CrossNewRelicClient.Current.RecordCustomEvent("XamarinCustomEvent", "XamarinCustomEventCategory", new Dictionary<string, object>()
{
{"BreadNumValue", 12.3 },
{"BreadStrValue", "XamBread" },
{"BreadBoolValue", true }
}
);
Record custom metrics (arbitrary numerical data).
CrossNewRelicClient.Current.RecordMetric("Agent start", "Lifecycle");
CrossNewRelicClient.Current.RecordMetric("Login Auth Metric", "Network", 78.9);
CrossNewRelicClient.Current.RecordMetric("Request Metric", "Network", 20, MetricUnit.SECONDS, MetricUnit.OPERATIONS);
Creates a session-level attribute shared by multiple mobile event types. Overwrites its previous value and type each time it is called.
CrossNewRelicClient.Current.SetAttribute("XamarinBoolAttr", false);
CrossNewRelicClient.Current.SetAttribute("XamarinStrAttr", "Cat");
CrossNewRelicClient.Current.SetAttribute("XamarinNumAttr", 13.5);
Increments the count of an attriubte. Overwrites its previous value and type each time it is called.
// Increment by 1
CrossNewRelicClient.Current.IncrementAttribute("XamarinNumAttr");
// Increment by value
CrossNewRelicClient.Current.IncrementAttribute("XamarinNumAttr", 12.3);
Removes an attribute.
CrossNewRelicClient.Current.RemoveAttribute("XamarinNumAttr");
Removes all attributes from the session.
CrossNewRelicClient.Current.RemoveAllAttributes();
Sets the event harvest cycle length.
CrossNewRelicClient.Current.SetMaxEventBufferTime(200);
Sets the maximum size of the event pool.
CrossNewRelicClient.Current.SetMaxEventPoolSize(1500);
Set a custom user identifier value to associate user sessions with analytics events and attributes.
CrossNewRelicClient.Current.SetUserId("User123");
Provides a HttpMessageHandler to instrument http requests through HttpClient.
HttpClient myClient = new HttpClient(CrossNewRelicClient.Current.GetHttpMessageHandler());
var response = await myClient.GetAsync(new Uri("https://jsonplaceholder.typicode.com/todos/1"));
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
} else
{
Console.WriteLine("Http request failed");
}
FOR ANDROID ONLY. Enable or disable collection of event data.
CrossNewRelicClient.Current.AnalyticsEventEnabled(true);
Enable or disable reporting successful HTTP requests to the MobileRequest event type.
CrossNewRelicClient.Current.NetworkRequestEnabled(true);
Enable or disable reporting network and HTTP request errors to the MobileRequestError event type.
CrossNewRelicClient.Current.NetworkErrorRequestEnabled(true);
Enable or disable capture of HTTP response bodies for HTTP error traces, and MobileRequestError events.
CrossNewRelicClient.Current.HttpResponseBodyCaptureEnabled(true);
Shut down the agent within the current application lifecycle during runtime.
CrossNewRelicClient.Current.Shutdown();
Sets the maximum size of total data that can be stored for offline storage.By default, mobile monitoring can collect a maximum of 100 megaBytes of offline storage. When a data payload fails to send because the device doesn't have an internet connection, it can be stored in the file system until an internet connection has been made. After a typical harvest payload has been successfully sent, all offline data is sent to New Relic and cleared from storage.
CrossNewRelicClient.Current.SetMaxOfflineStorageSize(200);
Logs an informational message to the New Relic log.
CrossNewRelicClient.Current.LogInfo("This is an informational message");
Logs an error message to the New Relic log.
CrossNewRelicClient.Current.LogError("This is an error message");
Logs a verbose message to the New Relic log.
CrossNewRelicClient.Current.LogVerbose("This is a verbose message");
Logs a warning message to the New Relic log.
CrossNewRelicClient.Current.LogWarning("This is a warning message");
Logs a debug message to the New Relic log.
CrossNewRelicClient.Current.LogDebug("This is a debug message");
Logs a message to the New Relic log with a specified log level.
CrossNewRelicClient.Current.Log(LogLevel.Info, "This is an informational message");
Logs a message with attributes to the New Relic log.
CrossNewRelicClient.Current.LogAttributes(new Dictionary<string, object>()
{
{"BreadNumValue", 12.3 },
{"BreadStrValue", "XamBread" },
{"BreadBoolValue", true },
{"message", "This is a message with attributes" }
}
);
This plugin provides a handler to record unhandled exceptions to New Relic. It is recommended to initialize the handler prior to starting the agent.
CrossNewRelicClient.Current.HandleUncaughtException();
if (Device.RuntimePlatform == Device.iOS)
{
CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE>");
} else if (Device.RuntimePlatform == Device.Android)
{
CrossNewRelicClient.Current.Start("<APP-TOKEN-HERE>");
}
This plugin also provides a method to manually record any handled exceptions as well:
try {
some_code_that_throws_error();
} catch (Exception ex) {
CrossNewRelicClient.Current.RecordException(ex);
}
The app may crash on iOS when recording a Null Pointer Exception.
Initialize the agent in AppDelegate.cs instead of App.xaml.cs.
Add the following code to your AppDelegate.cs file:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Mono.Runtime.RemoveSignalHandlers();
NRIosAgent.StartWithApplicationToken("<APP-TOKEN-HERE>");
Mono.Runtime.InstallSignalHandlers();
return base.FinishedLaunching(app, options);
}
Additional Information For more details on handling signals and third-party crash reporters, refer to the Mono documentation on Signals and third-party crash reporters."Mono Documentation"]
New Relic hosts and moderates an online forum where customers can interact with New Relic employees as well as other customers to get help and share best practices. Like all official New Relic open source projects, there's a related Community topic in the New Relic Explorers Hub. You can find this project's topic/threads here:
https://discuss.newrelic.com/tags/mobile
We encourage your contributions to improve [project name]! Keep in mind that when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.
If you have any questions, or to execute our corporate CLA (which is required if your contribution is on behalf of a company), drop us an email at opensource@newrelic.com.
A note about vulnerabilities
As noted in our , New Relic is committed to the privacy and security of our customers and their data. We believe that providing coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.
If you believe you have found a security vulnerability in this project or any of New Relic's products or websites, we welcome and greatly appreciate you reporting it to New Relic through HackerOne.
If you would like to contribute to this project, review .
To all contributors, we thank you! Without your contribution, this project would not be what it is today.
New Relic hosts and moderates an online forum where customers, users, maintainers, contributors, and New Relic employees can discuss and collaborate:
Except as described below, the newrelic-xamarin-plugin is licensed under the Apache 2.0 License.
The [New Relic XCFramework agent] (/docs.newrelic.com/docs/mobile-monitoring/new-relic-mobile-ios/get-started/introduction-new-relic-mobile-ios/) is licensed under the [New Relic Agent Software Notice] (/docs.newrelic.com/docs/licenses/license-information/distributed-licenses/new-relic-agent-software-notice/).
The [New Relic Android agent] (github.com/newrelic/newrelic-android-agent) is licensed under the Apache 2.0.
The New Relic Xamarin Plugin may use source code from third-party libraries. When used, these libraries will be outlined in .
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. monoandroid11.0 monoandroid11.0 is compatible. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. xamarinios10 xamarinios10 is compatible. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.