Wednesday, January 19, 2022

Exceptions filter

 public class CommonExceptionsFilter : ExceptionFilterAttribute

  {
      private readonly ILogger logger;
      public CommonExceptionsFilter(ILogger<CommonExceptionsFilter> logger)
      {
          this.logger = logger;
      }
 
      public override void OnException(ExceptionContext context)
      {
          context.HttpContext.Response.GetTypedHeaders().ContentType = null;
          bool isWarning = false;
 
          if (context.Exception is NotFoundException notFoundException)
          {
              isWarning = true;
              if(!string.IsNullOrWhiteSpace(notFoundException.Message))
              {
                  Dictionary<stringstring[]> output = new Dictionary<stringstring[]>();
                  output.Add("Message"new string[] { notFoundException.Message });
                  context.Result = new ObjectResult(output);
              }
              context.HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
          }
          else if (context.Exception is ArgumentException argumentException)
          {
              isWarning = true;
              Dictionary<stringstring[]> output = new Dictionary<stringstring[]>();
              // remove the last line from the message as it repeats the parameter name
              string paramName = string.IsNullOrWhiteSpace(argumentException.ParamName) ? null : argumentException.ParamName;
              string message = argumentException.Message.Contains(Environment.NewLine)
                  ? argumentException.Message.Substring(0, argumentException.Message.LastIndexOf(Environment.NewLine))
                  : argumentException.Message;
              output.Add(paramName ?? "Argument"new string[] { message });
              context.Result = new BadRequestObjectResult(output);
              context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
          }
          else
          {
              context.Result = new ObjectResult(new { Error = new string[] { context.Exception.Message } });
              context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
          }
 
          context.ExceptionHandled = true;
          if (isWarning)
          {
              logger.LogWarning(new EventId(context.HttpContext.Response.StatusCode), context.Exception, context.Exception.Message);
          }
          else
          {
              logger.LogError(new EventId(context.HttpContext.Response.StatusCode), context.Exception, context.Exception.Message);
          }
 
          base.OnException(context);
      }
  }

Friday, January 14, 2022

Why does nuget always ask for authentication?

 

  1. Goto https://companyname.visualstudio.com/Default/_packaging?_a=feed&feed=Feedname 
  2. Run the command in the directory where your package.json is present
    1. If the package.json is not present. Run the command npm init
    2. npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false  ( This will open an authentication window for your microsoft account)
    3. vsts-npm-auth -config .npmrc
  3. now run npm install and the package should be installed. 

Thursday, January 13, 2022

Application Insights with .net core and lambda.

 How to add application insights to AWS Serverless Lambda?


Need to specify the website_hostname 

and need to specify the APPINSIGHTS_INSTRUMENTATIONKEY

var functionProps = new FunctionProps()
{
Environment = new Dictionary<string, string>()
{
{ nameof(DeploymentOptions.ApiConfigKey), apiConfigKey},
{ "WEBSITE_HOSTNAME", scope.StackName },
{ "APPINSIGHTS_INSTRUMENTATIONKEY", Fn.ImportValue(ATS.AWSBootstrap.ExportNames.EmployeePlatformAppInsightsKey) },
},
Timeout = Duration.Minutes(1),
MemorySize = 1024,
};



{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information"
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "000000000000000000000000000000"
  }
}