Let me know if this work , obviously code will not compile, but should give you an idea as to what we are trying to do.
using Elastic.Apm;
using Elastic.Apm.Api;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace MWriter
{
public class Writer
{
//using data from App config file using Properties.Setting
public bool SendEventMsgToTopic(MsgEvent msgEvent)
{
//Elastic ITransaction
ITransaction tran = null;
try
{
bool allGood = CheckEnvValues();
if (allGood)
{
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URLS", Properties.Settings.Default.APMSERVERURL);
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", Properties.Settings.Default.APMSERVICENAME);
//APM agent
tran = Agent.Tracer.StartTransaction("WriteMessage", ApiConstants.TypeExternal);
connection = CreateConnection();
if (connection != null)
{
session = CreateWriter(connection);
if (session != null)
{
string message = JsonConvert.SerializeObject(msgEvent);
....
}
else
{
errorMsg = "Null objet Error in session creation";
Console.WriteLine(errorMsg);
UpdateAPM(tran, errorMsg);
return success;
}
}
else
{
errorMsg = "Invalid values in Application Config, check app config";
Console.WriteLine(errorMsg);
UpdateAPM(tran, errorMsg);
return success;
}
}
catch (Exception ex)
{
//errorMsg = ex.InnerException.ToString();
errorMsg = "Error Occured";
tran.CaptureException(ex);
UpdateAPM(tran, errorMsg);
throw;
}
finally
{
dispose off connection
}
}
private static void UpdateAPM(ITransaction tran, string errMsg)
{
tran.Result = "Error";
tran.End();
}
private static IConnection CreateConnection()
{
bool allValuesRead = CheckEnvValues();
IConnection connection = null;
ITransaction tran = Agent.Tracer.StartTransaction("CreateConnection", ApiConstants.TypeExternal);
try
{
//set the domainName
tran.Labels["Domain"] = Properties.Settings.Default.DomainName;
.........
catch (Exception ex)
{
tran.CaptureException(ex);
errorMessage = ex.InnerException.ToString();
UpdateAPM(tran, errorMessage);
throw;
}
return connection;
}
}