The hidden gotcha with IObjectInstanceCache

It’s not a secret that cache is one of the most, if not the most, important factors to their website performance. Yes cache is great, and if you are using Optimizely Content/Commerce Cloud, you should be using ISynchronizedObjectInstanceCache to cache your objects whenever possible.

But caching is not easy. Or rather, the cache invalidation is not easy.

To ensure that you have effective caching strategy, it’s important that you have cache dependencies, i.e. In principles, there are two types of dependencies:

  • Master keys. This is to control a entire cache “segment”. For example, you could have one master key for the prices. If you needs to invalidate the entire price cache, just remove the master key and you’re done.
  • Dependency keys. This is to tell cache system that your cache item depends on this or that object. If this or that object is invalidated, your cache item will be invalidated automatically. This is particularly useful if you do not control this or that object.

ISynchronizedObjectInstanceCache allows you to control the cache dependencies by CacheEvictionPolicy . There are a few ways to construct an instance of CacheEvictionPolicy, from if the cache expiration will be absolute (i.e. it will be invalidated after a fixed amount of time), or sliding (i.e. if it is accessed, its expiration will be renewed), to if your cache will be dependent on one or more master keys, and/or one or more dependency keys, like this

   
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheEvictionPolicy"/> class.
        /// </summary>
        /// <param name="cacheKeys">The dependencies to other cached items, idetified by their keys.</param>
        public CacheEvictionPolicy(IEnumerable<string> cacheKeys)

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheEvictionPolicy"/> class.
        /// </summary>
        /// <param name="cacheKeys">The dependencies to other cached items, idetified by their keys.</param>
        /// <param name="masterKeys">The master keys that we depend upon.</param>
        public CacheEvictionPolicy(IEnumerable<string> cacheKeys, IEnumerable<string> masterKeys)

The constructors that takes master keys and dependency keys look pretty the same, but there is an important difference/caveat here: if there is no dependency key already existing in cache, the cache item you are inserting will be invalidated (i.e. removed from cache) immediately. (For master keys, the framework will automatically add an empty object (if none existed) for you.)

That will be some unpleasant surprise – everything seems to be working fine, no error whatsoever. But, if you look closely, your code seems to be hitting database more than it should. But other than that, your website performance is silently suffering (sometimes, not “silently”)

This is an easy mistake to make – I did once myself (albeit long ago) in an important catalog content cache path. And I saw some very experienced developers made the same mistake as well (At this point you might wonder if the API itself is to blame)

Take away:

  • Make sure you are using the right constructor when you construct an instance of CacheEvictionPolicy . Are you sure that the cache keys you are going to depend on, actually exist?

In newer version of CMS, there would be a warning in log if the cache is invalidated immediately, however, it could be missed, unless you are actively looking for it.

Note that this behavior is the same with ISynchronizedObjectInstanceCache as it extends IObjectInstanceCache.

Lelit Elizabeth v3 – 2 months impression (super short review)

First of all, if you are looking to buy Lelit Elizabeth, check out the detailed review from David Corbey. It is probably the best review on the machine. He also wrote very good information regarding setting, and maintaining the machine, so check it out.

Out of the box, the Elizabeth looks much better than in photos. I must admit, the photos do not do its justice – it looks rather dull in those. While it is certainly not the best looking espresso machine (to my taste at least, I think the one with E61 group looks more compelling), it definitely looks good.

Next to the machine it is going to replace

Coming from the popular Sage (Breville) Barista Pro, there are a few things that impress/surprise me (of course this is an unfair comparison. When you spend 3x more money on a new machine + a grinder, you would definitely not want to get “marginally better”):

  • The actual stainless steel construction is very, very nice. It feels much more solid than the fake stainless steel look from the Barista Pro. The machine is well made, probably not the best built machine around, but you know it will last you a long time.
  • It takes really long time to heat up, about 20 minutes. Compared to Barista Pro, it was a bummer at first, because you could pull your first shot almost instantaneously with the Pro as the machine is ready after 3s. But that is a lie. For the Barista Pro, the machine allows you to pull shots, but with the cost of temperature stability. I learned the hard way that most of the shots with Barista Pro is severely under temperature, resulting in extreme sour taste, and it was very hard to adjust – you have like 5 levels for temperature, ranging from 90*C to 98*C, so about 2*C each. If the machine can reach that desired temperature or not, is another question. I only realize that once I switched to Elizabeth. The portafilter is actually hot (and very uncomfortable to touch to steel part). On Barista Pro however, it is only lukewarm, even if I pulled a few empty shots before hand. Furthermore, with Elizabeth, I can set the brewing water to whatever temp I like (or half the degree if I switch to F instead of C), so I can comfortably brew light, medium or dark roast the way they are meant to be brewed.

The flow is now so, so much easier and smoother and I had with the Sage Barista Pro.

Buying accessories is also now easier and cheaper – I could easily find branded, quality accessories for reasonable prices. They might be still expensive, but I feel the price is justified for the the quality.

But no machine is perfect, so is Elizabeth, there are a few downsides

The biggest one, to me at least, is that the water tank is pretty hard to refill, as I put my machine under the kitchen cabinet, so I have a few little space left. I need to either move it out, or use a gooseneck kettle to refill. I went with the latter approach and it works quite well.

Another bummer is that there is lack of a real tamper included in the package. Lacking of the milk jug is somewhat acceptable, but tamper? I bought a nice one from Motta (wish I chose the 58.55mm version instead), but I wish they included one by default. Of course, this one is an easy one to fix.

In the end, I’m happy with my Elizabeth, and I feel happy and excited to use it every day. My only regret is that I didn’t step up to Bianca – heard great things about it. But well, it exceeded my budget at that point by a large margin, I’ll have to wait.

Meanwhile, Lelit Elizabeth will serve me well for a long, long time.

Potential performance issue with Maxmind.db

From time to time, I have to dig into some customers’ profiler traces to figure out why their site is slow (yeah, if you follow me, you’d know that’s kind of my main job). There are multiple issues that can eat your website performance for breakfast, from loading too much content, to unmaintained database indexes. While my blog does not cover everything, I think you can get a good grasp of what mistakes to avoid.

But sometimes the problem might come from a 3rd party library/framework. It’s not new, as we have seen it with A curious case of memory dump diagnostic: How Stackify can cause troubles to your site – Quan Mai’s blog (vimvq1987.com). The problem with those types of issues is that they are usually overlooked.

The library we’ll be investigating today would be Maxmind.db. To be honest, I’ve never used it my own, but it seems to be a very popular choice to geography-map the visitors. It’s usually used by Optimizely sites for that purpose, using VisitorGroup (which is why it came under my radar).

For several sites that use it, it seems more often than not stuck in this stack

It’s essentially to think that CreateActivator is doing something heavy here (evidently with the LambdaCompiler.Compile part. A peek from decompiling actually shows that yes, it’s heavy. I’m not quite sure I can post the decompiled code here without violating any agreement (I did, in fact, accepted no agreement at this point), but it’s quite straightforward code: TypeActivatorCreator uses reflection to get the constructors of the Type passed to it, to sees if there is any constructor decorated with MaxMind.Db.Constructor attribute, then prepares some parameters, and creates an LambdaExpression that would create an instance of that Type, using found constructor (which is a good thing because a compiled expression would perform much better than just a reflection call).

(I’m using Mindmax.db 2.0.0, for the record)

The code is straightforward, but it is also slow – as any code which involves reflection and lambda compilation would be. The essential step would be to cache any result of this. This is actually a very good place to cache. The number of types are fixed during runtime (except for very edge cases where you dynamically create new types), so you won’t have to worry about cache invalidation. The cache would significantly improve the performance of above code.

And in TypeActivatorCreator there is a cache for it. It is a simple ConcurrentDictionary<Type, TypeActivator> , which would return an TypeActivator if the Type was requested before, or create one and cache it, it it hasn’t been. As I said, this is a very good place to add cache to this.

There is a cache for that, which is good. However, the very important tidbit here is that the dictionary is not static. That means, the cache only works, if the class is registered as Singleton (by itself, or by another class down the dependency chain), meaning, only one of the instance is created and shared between thread (which is why the ConcurrentDictionary part is important).

But except it’s not.

When I look at a memory dump that collected for a customer that is using Maxmind.db, this is what I got:

0:000> !dumpheap -stat -type TypeAcivatorCreator
Statistics:
MT Count TotalSize Class Name
00007ffa920f67e0 1 24 MaxMind.Db.TypeAcivatorCreator+<>c
00007ffa920f6500 147 3528 MaxMind.Db.TypeAcivatorCreator
Total 148 objects

So there were 147 instances of TypeAcivatorCreator. Note that this is only the number of existing instances. There might be other instances that were disposed and garbaged by CLR.

Now it’s clear why it has been performing bad. For supposedly every request, a new instance of TypeActivatorCreator is created, and therefore its internal cache is simply empty (it is just newly created, too). Therefore each of request will go through the expensive path of CreateActivator, and performance suffers.

The obvious fix here is to make the dictionary static, or making the TypeActivatorCreator class Singleton. I don’t have the full source code of Mindmax.Db to determine which is better, but I’d be leaning toward the former.

Moral of the story:

  • Caching is very, very important, especially when you are dealing with reflection and lambda compilation
  • You can get it right 99%, but the 1% left could still destroy performance.

Update:

I reached out to Maxmind.db regarding this issue on November 9th, 2021

About 6h later they replied with this

I was at first confused, then somewhat disappointed. It is a small thing to fix to improve overall performance, rather than relying on/expecting customers to do what you say in documentation. But well, let’s just say we have different opinions.

Debugging a memory dump for .net 5

You would need to install Windbg Preview from Windows Store

Get it from Get WinDbg Preview – Microsoft Store . If you use the ordinary Windbg that comes wint Windows SDK, this is what you get from trying to open it

WinDbg:10.0.19041.685 AMD64

Could not find the C:\Users\vimvq\Downloads\core_20211102_090430.dmp Dump File, Win32 error 0n87

The parameter is incorrect.

You also need to install .NET 5 version of sos.

dotnet tool install --global dotnet-sos

and once you used Windbg Preview to open the memory dump, run this command to load it:

.load C:\Users\<your user name>\.dotnet\sos\sos.dll

And now you can start debugging as usual

Coffee roasters in Stockholm – a review

If you are serious about Espresso quality, you know you must buy from a specialty roaster – not from the super market. You will pay more premium price, at least 300kr per kg, and easily up to 500kr per kilo or more, compare to around 100-150kr per kg from super market. In return, you get:

  • The obvious better coffee quality. Most if not all decent roasters only roast specialty coffee, meaning they not only taste good, they have minimal defects, especially small rocks. A bad bean will only ruins one cup, at most. But a pebble can destroy your precious coffee grinder. Your grinder will thank you for the uniformity of specialty coffee beans you buy from roasters.
  • Much better freshness. Most, if not all coffees from super market only have “expired date”, not “roast date”. You can probably guess the roast date by subtracting expired date by 24 months and the most fresh one I could find, was two months old. At this point the coffee already started degrading in quality. In contrast, when I buy from roaster, it is always less than 1 week from roast date, which is clearly printed on the bag. As a rule of thumb, you should finish your coffee in less than 8 weeks from roast date.
  • Much better roasted. Most coffee from super market is roasted with super hot air (800*c) in very short amount of time. This allows the roaster to roast ton after ton, but with the cost of coffee flavor. Specialty coffees are often roasted in much smaller batches, an a longer time, for the flavor can develop properly.
  • Traceability. You only know coffee from super market by their country origin, and that’s it. But for specialty coffee, you will know the region which produced the coffee, and in many cases, even the farm that produced it.
  • Last but not least, support for local businesses. Specialty roasters are small businesses in your city, or even area. Buying from them means you support your local economy. Many roasters also have direct trade with the coffee farms, which means you will more directly support those famers. Most farms that grow specialty coffee also follow practices regarding sustainability (and due to high price of specialty coffee, they can sustain their business with considerable smaller farms). If you care about sustainability and people likelihood, buying from roasters is a better way to support that.

What to look for from Coffee roasters

A coffee bean bag with one way valve is a must (In case you didn’t know, newly roasted coffee bean will release CO2, and that valve is important to let the CO2 out – but not let the air in) . Best if it is resealable. otherwise you would have to move the bean to an airtight container to keep them fresh for longer. Also, buy coffee beans if you can. Ground coffee starts losing their aroma and flavor just 30 minutes after grinding. Airtight container can only slow that down a little bit.

All of the roasters below have good coffees – the beans have consistent color, size and shape. My machines, techniques and taste are not at the level I can distinct each flavor, so I will focus on the services instead.

StockholmRoast (Stockholm Roast – The House of Roasting)

They offers good prices, but no subscription. They ship through DHL to the service point, with free shipping, which is nice, but not the best (compared to other options below)

The bags are well packaged, so you get proper protection of your precious coffee beans. But they also glue a plastic bag on the package (for the shipping information and the receipt), which is quite tiresome to remove for proper recycling.

They also offer better price for 1kg bag, compared to 4 bags of 250. While this is somewhat understandable from a commercial perspective, it means it’s harder to keep your coffee fresh, if you want to save some money. They probably should offer 500gr bag.

Another minus, I don’t recall their bag is resealable. Also the ink on the bag could easily get into you hands, especially if they are a little wet. They are, however, not very easy to wash off.

None of those things are critical, but they would be very nice to be fixed!

LYKKE KAFFEGÅRDAR Nyrostat kaffe | Direkt från gården hem till dig | Lykke Kaffegårdar (lykkegardar.se)

Lykke offers subscriptions, with 10% discount, which is good. Importantly, they ship directly to your mailbox. Order, and in one day or two, you find your favorite coffee bags in your mailbox. Convenient, huh?

You can easily manage subscriptions, including changing it, skip one delivery, or cancel it, which is a huge plus.

Their bag design is beautiful, and I absolutely like it. To make things better, they even included 2 bags of tea in my first shipment – a very good way to advertise.

Their espresso range, however, is quite limited.

Kafferosterietkoppar Upptäck vårt nyrostade kaffe | Kafferosteriet Koppar

They offer subscriptions as well, and with 20 SEK discount per bag, which is very nice. However, to change the subscription, you need to email them directly. It’s OK-ish, but I would definitely prefer the Lykke approach.

They also ship directly to mailbox, and their shipping was very fast. I ordered on Wednesday, and two of the bags appeared in my mail box on Thursday. I don’t know if they forgot, or intentionally did not send a notification email, but that was a nice surprise.

One incident with my first purchase: Out of two bags is almost empty (there were like, 30 coffee beans inside them). I mailed them to let they know, and they were happy to ship a replacement to me. In the end, everything is resolved quick and easy, but I’d hope they did have a bit more of quality control for their coffee bag.

Black Honey blackhoneycoffeeroasters.se

At this point of writing, all of the coffees at Single Origin – Whole beans – Black Honey Coffee Roasters are out of stock. I don’t know if they are still in business or not, but that’s such a bummer.

Standout coffee https://www.standoutcoffee.com/

Their subscription is 25e (yes, euro, equivalent to about 260kr) for 100gr of coffee, or 2600kr per kilo. The reason for such high price is because it’s “Gesha village”, the most expensive coffee in the world, and they offer worldwide free shipping.

2600kr per kilo is unfortunately way too high for what I can pay for coffee, and with 100gr you might get 1-2 cup of good espresso out of it (considering you have to dial in), so thanks, but no thanks.

Drop Coffee

Apparently they are the most popular in Sweden, so I should try them out soon. They are transparent about their FOB price, which is nice. I was hesitant about their Google reviews (“only” 4.3 on 5.0, so quite lower than other roasters in this list), but it turned out it has to do with their coffee shops (which should be affected by many other things) than their actual roasting business.

Next up: morgoncoffeeroasters

Sage/Breville Barista Pro review

This is a super short review of this fairly popular espresso machine. I bought this last year, despite a lot of arguments from my wife. She even threatened to throw it out if I bought. I did. And now she demands latte/cappuccino every day!

My budget was pretty limited at that point, so other decent options (HX or even dual boiler machines) are out of reach. Barista Pro fits in my budget (and kitchen), and when Amazon had a very good discount on them, I pulled the trigger.

I was happy.

When it was new

Don’t laugh.

Pros:

Sage/Breville is feature oriented, and when you open the box, you have everything you need to get going: a milk jug, a 54mm portafilter with 4 different baskets (2 double shots (1 pressurized, 1 non pressurized), 2 single shot), and of course, a grinder built-in. If you are new, this is hugely important. Some sellers do not include the milk jug, or even tamper (looking at you, Lelit!), and it’s bad that you are excited to open your new fancy espresso machine and realize you can’t make a decent cappuccino due to lacking equipment. The UI is intuitive and easy to work with. Once you understand the basics, using the machine, UX wise, is simple and easy.

The flow is well defined, and smooth – you take the portafilter, put it in the holder and click – the grinder grinds coffee for you, in the fineness you chose and the time you pick. Then you take the tamper (attached to the machine using a magnet, a pretty smart design), tamp it, put it in the head, place your cup, and press a button. It is the convenience you are paying for.

Cons:

Once you open the box, you quickly realize this machine is not built to last. It’s a thin layer of stainless steel outside of plastic. Build quality is … fine, but don’t expect the same quality as Italy-made machine. It’s been reported that while Sage/Breville service is very good during warranty, but one you are out of warranty, you have to pay hefty fee for repairs, because they just break down. And repair usually means “replace”.

The machine is advertised as “3s start up time”. You press a button, and the machine is ready. Truth is, however, if you want to get better shots, you need to wait for at least 10m, and flush 1 or 2 cup first. With the empty portafilter inserted, press the double shots button, and let the hot water flows through it. It warms up the head, the portafilter, and make sure you get stabilized temperature in the boiler. Otherwise, your cups will be incredibly sour. Or some times, both sour of bitter!

The machine overall is quite noisy, both the grinder and the pump. it’s not a big deal until you have tried quieter machines. This is even more true when you have empty grinder, it sounds like it gonna break (it is fine to grind in a short amount of time, mind you)

Another downside is that this uses a 54mm portafilter. The portafilter itself is fine, well made and solid, but after a while, you will want to try out new things, like bottomless portafilter. But this is when you realize you are left with either options: 1. buy cheap no brand products from China or 2. absurdly expensive or 3. both. Should it come with a 58mm portafilter which is the “industry standard”, you will have more options from reputable brands, at reasonable prices.

The built-in grinder is merely adequate, it’s step conical burr grinder (some says it’s actual stepless, but you will need some “tricks” for that). You will be able to grind espresso with it, but not with the fineness adjustment needed to extract the best out of your coffee. Whenever you can, upgrade to a good espresso grinder would make a huge difference in your espresso. (Note: a good espresso grinder can easily cost $400 or more!). Also, cleaning it is not the easiest task – it’s doable, but requires additional tools (like a vacuum cleaner) to do it properly.

It’s messy to grind a double shot (18-20gr), because some coffee ground will be left on the portafilter holder, or on the drip tray. Yes you can use a dosing cup, or a funnel, but you will, once again, agonize the limited options of a 54mm portafilter.

So so

The included tamper is “serviceable”. It can be tucked in which is need, and it does it job. But I’d suggest to buy a nice, ergonomic tamper as soon as you can. It’ll make your experience much more enjoyable.

The steam wand is ok, but it is on the weak side, and it produces wetter steam than I would like. It is enough to froth the included milk jug, but if you want to use a bigger jug (so you can make 2 cappuccinos or 1 big latte in 1 go), it’ll not powerful enough.

The included milk jug is OK. Good ergonomic, but the wall is a bit too thin, so it gets hot very quickly. I had hard time holding it when it reaches 55*C. In comparison, my Motta one is only fairly warm even when the milk reaches 60*C (that is however not the perfect thing)

Summary

In the end, Barista Pro is a well rounded, full featured espresso machine. It’s a budget/entry one, capable of making good shots. You have everything you need to start going, but it also does not really excel in neither brewing, nor steaming. Once you horned your skill, upgrading to a better grinder, and a better machine 58mm portafilter will be a big step.

If you want to learn and can spend, of course.

How to spot Facebook Marketplace scams

With the pandemic going for more than 1 year (And still no end in sight), shipping has become a popular option for shopping on Facebook Marketplace. And that’s why scamming skyrocketed. To make the matters worse, Facebook provides little to no shopper protection. Your safety is yours to care about. So how to spot and stay away from scam, beside of other health safety procedures?

If a deal seems to be too good to be true, it is likely is. At this point of writing, PlayStation 5 is most frequently scammed. Anything which is less than 5500 SEK for the standard edition is suspicious. Scalpers who sell high demand product at much higher price than MSRP are another issue, but they are not mutually exclusive. A scammer can try to sell at higher price to make it more “authentic”, but most will try to lure more unsuspecting buyers by low prices.

Photos with low quality are another red flag. It is likely that the seller did not take the photos himself/herself but download from other listing, then upload again. Every time you upload photos to Facebook they reduce the quality a bit to reduce size. Do that a few times and you will notice the artifacts in the photo. If the photo is blurry or pixeled, take that as a warning.

Commerce profile too new: anything newer than 2019 should be questionable. Especially someone with very few friends. Check their profile if you think they are an actual person behind it.

Only offer shipping is another red flag. While some authentic sellers would prefer less contact, it is uncommon. Shipping means you have to Swish (a quick money transfer method in Sweden) before hand, and that means you have no guarantee once you did. If you Swish

Also, check if that seller is selling multiple items, and for each item, a new location is listed. That means the seller is trying to scam you into thinking he/she is far away, so shipping is required.

An example of a scammer

My not very proud experience is that I was scammed myself. I was browsing the MarketPlace, and saw a game (Ratchet & Clank PS5), for a very good price (250kr compared to normally 450-500kr or even more, because the game was new). Contacted the seller, he was responsive, and even offered 50:50 split on shipping cost. I checked his profile, everything seems to check out, so I took the bait. Swish-ed him 270kr. Waited for a few days. The game never came. Ask him if he shipped it, he said yes. Waited for a few days more and asked him what would he do. He never replied. Only a few days later, another buyer reported him for scamming. Same tactic, only this time he admitted to that buyer he never sent the game (a different one), and promised to refund the money. He never did.

Deep down, I wanted him to not be scammer. I even felt a bit sorry for him. What state would he be in, so he has to scam over that small amount of money. Only after a while, I realized he did it systematically. He scams that amount so most people will just accept it and move on. Even if they report him to the police (as the said buyer did), they will likely not do anything about it. He gets away with scamming multiple people like that.

Facebook protection for buyers is a joke. The most you can do is to report the listing as Scam, and they still do nothing about it. So better be cautious and protect yourself.

HP inkjet printer is a scam

I occasionally need to print something, and based on the reviews in rtings.com, a HP OfficeJet Pro 9013 was probably my best bet for performance/price (given the top choice is not available anywhere in my region – Europe/Sweden). Researching showed some shady business practice by HP for their inkjet printers, but against my best judgement, I decided to try my luck.

My mistake.

The printer worked fine for a while. Nothing particularly good or bad about it. It prints, it scans, it fits what I need. I bought it new at a reasonable price, and got 3 years commercial warranty which seems like a good deal.

But the honeymoon does not last long.

The yellow cartridge went out first. It does not let me print even black and white document.

Why do I need yellow when I only want to print B&W?

This is crappy business, and I was warned, but OK fine. I signed on for this. So I bough the yellow cartridge to replace the old one. All good? No, this time, it’s cyan that is out of ink.

You can see Magenta is still there

Even without printing one page.

I am forced to buy cyan. Just to print one page, in black and white.

And then, when cyan cartridge arrives, I replaced the old one, and guess what, magenta is out!

Until it is magically out

Even without printing one page.

I can’t help but to heavily suspect that HP does that intentionally.

The printer fails to do its job when you need it, sometime sorely. This entirely defeats the convenience of having your own printer at home. I could have just printed it by a service, and be done with it.

I called HP support and told that “the cartridges should be replaced at the same time”, and “just get a new cartridge and it will work”. You can see, that is less than helpful.

This is simply a terrible, terrible practice from HP (and sadly it’s not uncommon in the business)

I learned my lesson – never again.

Announcing Pro Optimizely Commerce Cloud

Yes, you guess it right, it’s a(nother) book

It has been 5 years since I started Pro Episerver Commerce back in early 2016. The book was a success, not as big as I hoped for, but definitely bigger than I expected. Tackling a niche market, it was fairly popular within the community, and it gave me a lot of happiness (and some pocket changes) to see that it helped many developers to understand and use the framework – which I help created, and love – better.

So much has changed in the last 5 years.

I have my first kid, and a second one. I left Commerce development team, to work on my own, then have a small team. Episerver bought Optimizely, then rebrand.

And so much more has happened with Episerver Commerce, more than just being renamed to Optimizely Commerce Cloud.

It deserves a new book!

To celebrate my 10th anniversary with Episerver (now Optimizely), I am proud, and excited to announce the second edition of Pro Episerver Commerce – Pro Optimizely Commerce Cloud. Most of the content written in Pro Episerver Commerce is still very much applicable, but I feel there is a need to refocus and expand on important parts.

You can register your interests today at https://leanpub.com/prooptimizelycommercecloud

Purchasers of Pro Episerver Commerce – even if you obtained the free version – will receive a 40% discount code for the new book – so don’t miss it.

I will see you there!

Lessons learned about exception handling and logging

Exception handling and logging is essential part of any site. Your site will eventually run into problems, and as a developer it’s your job to make sure that you have enough – or at least helpful – information to look into problems. From my years of diagnosing and root cause analysis, these are the most common problems about exception handling and logging. You (or any developers that come after you) can thank yourself later.

Empty try catch is almost always bad

In my early days of professional programming, I saw a colleague – several years senior to me, fortunately, not at Episerver – write a lot of this code, basically in each and every method:

try { 

//do stuffs

}

catch {}

You surely don’t want to show a YSOD screen to your visitors, but keep this in mind – this is almost always bad. You are trying to hide errors, and even worse, trying to swallow it. This should be a red flag for any code review.

There are cases when you truly, really want to swallow an exception. It’s rare, but it’s a reality. In such case, make sure to explain why you have an empty catch in comments.

Don’t catch exception only to throw it

Does this look familiar to you, if yes, then your code base have a problem (or actually, two)

try 
{
//do stuffs
}
catch {Exception ex}
{
throw ex;
}

But why?

  • It is wasting a try catch doing nothing of value. Log in, wrap it in a different type of exception (with probably more information, not just rethrow the exception
  • throw ex; actually reset the stacktrace. If you simply want to rethrow the exception (after doing meaningful stuffs with it), use throw; instead. That will preserve the precious stacktrace for exception handling at higher level.

Logging only the message is a crime

Someday, you will find an entry in your log looks like this

Object reference not set to an instance of an object.

And that’s it. This is even worse than no message – you know something is wrong, but you don’t know why, or how to fix it. It’s important to always log the full stacktrace of the exception, instead of just the message, unless you have a very good reason (I can’t think of one, can you?) not to.

Verbose is better than concise

This is an extension of the above lesson. You are already logging the entire stacktrace, but is your exception message helpful? One example is this

[InvalidOperationException: There is already a content type registered with name: PdfFile]
   EPiServer.DataAbstraction.Internal.DefaultContentTypeRepository.ValidateUniqueness(ContentType contentType) +222
   

Which other type was registered with same name? Is it a built-in (i.e. system) type, or a type from a 3rd party library, or a type that you added but forgot yourself?

In this case, a much better exception message would be:

There are two or more content types registered with name PdfFile

Assembly1.Namespace1.PdfFile
Assembly2.Namespace2.PdfFile

Exception message is not only showing an error/unwanted situation has happened, but it needs to be helpful as well. When you log a message, you should try to add as much information as you can so the ones who will be looking into the issue can make a good guess of what is wrong, and how to fix it. There is virtually no limit on how verbose you can be, so feel free to add as much information as you need (I’m not asking you to add the entire “War and Peace” novel here, of course). You can thank me later.