Using ELMAH's SqlErrorLog to manually insert into Sql Server

by sunil ravulapalli /27. March 2013 00:47 /.net-tools /Comments (0)

This is a class I use to manually log into ELMAH tables

using Elmah;

namespace WesternUnionService
{
 public class Logger
 {
  public static bool LogError(Exception exception)
  {
	Error error = new Error()
	{ 
		HostName = System.Environment.MachineName,
		Type = exception.GetType().ToString(),
		Source = exception.Source,
		Message = exception.ToString(),
		StatusCode = 0,
		Time = System.DateTime.UtcNow
	};

	Log(error);

	return true;
  }

  public static bool LogInfo(string info)
  {
	Error error = new Error()
	{ 
		HostName = System.Environment.MachineName,
		Type = "Info",
		Source = ConfigurationManager.AppSettings["applicationName"],
		Message = info.Length <= 500 ? info : info.Substring(0,500),
		StatusCode = 0,
		Time = System.DateTime.UtcNow
	};

	Log(error);

	return true;
  }

  private static void Log(Error error)
  {
	  SqlErrorLog sqlErrorLog = new SqlErrorLog((Hashtable)ConfigurationManager.GetSection("elmah/errorLog"));
	  sqlErrorLog.Log(error);
  }

 }
}

It can be used like below:

try
{
 //do something
}
catch (Exception ex)
{
 Logger.LogError(ex);
}

or

Logger.LogInfo("Peek a boo");

Mocking session variables using Moq

by sunil ravulapalli /22. March 2013 00:20 /.net-tools /Comments (0)

If we want to test a controller method which uses session variables like the following example, we can use the Moq library to Mock up the session variables.

 
public ActionResult ShowBalance()
{
  var accountId = Convert.ToInt64(Session[SessionConstants.AccountId]);
  var last4DigitsOfSsn = Session[SessionConstants.Last4DigitsOfSsn].ToString();
  AccountStatus accountStatus = _quickPayService.ValidateAccount(accountId, last4DigitsOfSsn);
  if (accountStatus.IsValid)
    return View(accountStatus);
  else
    return RedirectToAction("ShowError", accountStatus );
}

Below is an example of how session variables can be mocked

[TestMethod]
public void ShowBalance_WithNonExistantAccount_GoesToShowError()
{
  //Arrange
  Mock<HttpSessionStateBase> session = new Mock<HttpSessionStateBase>();
  session.SetupGet(s => s["AccountId"]).Returns(99999999);
  session.SetupGet(s => s["Last4DigitsOfSsn"]).Returns("9999");
Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>(); httpContext.SetupGet(c => c.Session).Returns(session.Object);
ControllerContext ctx = new ControllerContext(); ctx.HttpContext = httpContext.Object;
var quickPayServiceMock = new Mock<IQuickPayService>(); quickPayServiceMock.Setup(quickpayservice => quickpayservice.ValidateAccount(99999999, "9999")).Returns(new AccountStatus { IsValid = false }); KioskController controller = new KioskController(quickPayServiceMock.Object); controller.ControllerContext = ctx; //Act RedirectToRouteResult redirectToRouteResult = controller.ShowBalance() as RedirectToRouteResult; //Assert Assert.AreEqual("ShowError", redirectToRouteResult.RouteValues["action"].ToString()); }

Error loading module 'Ninject.Web.Mvc.MvcModule' of type MvcModule Another module (of type MvcModule) with the same name has already been loaded

by sunil ravulapalli /5. March 2013 00:58 /.net-tools /Comments (2)

Error loading module 'Ninject.Web.Mvc.MvcModule' of type MvcModule
Another module (of type MvcModule) with the same name has already been loaded
Suggestions:
 1) Ensure that you have not accidentally loaded the same module twice.
 2) If you are using automatic module loading, ensure you have not manually loaded a module
    that may be found by the module loader.

While using NINJECT, If you are getting this error when using 

kernel.Load(AppDomain.CurrentDomain.GetAssemblies());

one possible solution might be is to do it this way, by listing out the dll's individually

kernel.Load("projectname.data.dll");

kernel.Load("projectname.service.dll");

I haven't figured out why the modules were loading more than once, but the above work around does achieve the "loose coupling" we seek.

 

 

Could not load file or assembly 'NHibernate, Version=2.1.0.4000

by sunil ravulapalli /16. June 2010 20:43 /.net-tools /Comments (0)

I got this error when I was trying out Linq to NHibernate.

Looks like I was using an older version of NHibernate.Linq.dll(1.0) with NHibernate.dll (2.1.2.4000).
So I downloaded NHibernate.Linq.dll version 1.1.0.1001, removed my old NHibernate.Linq.dll reference and added this to make my application work.
To get that dll, search for NHibernate.Linq-2.1.2-GA-Bin.zip and extract it using WinRar( WinZip did not work) if you are having similar problems.

NHibernate.MappingException : No persister for

by sunil ravulapalli /16. June 2010 19:57 /.net-tools /Comments (0)
I got this error when I was adding new functionality to my "Teach myself NHibernate" application.
I binged, googled for several hours....
Some of popular suggestions include, setting the xml file as an "Embedded resource", checking for typos, making the classes public, but those were not my problems. Most of them solved their problem easily by fixing one of those things. But my problem wasn't that simple or so I thought.
 
I tried everything under the sun and finally it came to me that I named my file PRODUCT.XML instead of PRODUCT.HBM.XML.
 
 

NHibernate Configuration for Oracle using Microsoft Driver

by sunil ravulapalli /11. May 2010 23:11 /.net-tools /Comments (0)


NHibernate.Connection.DriverConnectionProvider

NHibernate.Dialect.Oracle9iDialect
NHibernate.Driver.OracleClientDriver

Data Source=x;User Id=xxx;Password=xxx;


NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle

true



1. Take note of the "9i" in "dialect" property. 2. Add reference to System.Data.OracleClient in the .Net project.

How to use Form in Watin

by sunil ravulapalli /11. May 2010 23:08 /.net-tools /Comments (0)
Firefox fox = new Firefox("http://***********.com");
Form f = fox.Form(Find.ByName("MyForm"));
f.TextFields[0].TypeText("niko");
f.TextFields[1].TypeText("bellic");
f.Images[0].Click();