Our Gemara on Amud Aleph quotes the famous teaching of Rav Gidal, that even though ordinarily one should refrain from making oaths, for a Mitzvah it is permitted. Presumably, if one needs to galvanize and commit to a Mitzvah, making an oath is permitted (See Nedarim 7a).

There is a fascinating machlokes Rambam

And Ramban in Sefer Hamitzvos, Aseh 7. The Rambam states:

⁦That is that He commanded us to swear by His name when it is necessary to ratify something or to deny it. For there is glory and exaltation to God through this. And that is His, may He be blessed, saying, "and by His name shall you swear" (Deuteronomy 10:20). And in the explanation, they said, "The Torah said, 'Swear,' and the Torah said, 'Do not swear' - meaning to say, just like it prohibits an oath for which there is no need, and it is a negative commandment; so too is there a commandment [to make] an oath when it is needed, and it is a positive commandment. 

Ramban cannot abide by this idea that it could be a Mitzvah to use God’s name in an oath. Indeed, we find in the Gemara great reluctance to make an oath, where pious people would pay judgments in civil matters even when innocent in order to avoid making an oath. (See Shulkhan Arukh CM 97:21 and Pisechei Teshuva Op. Cit.) Thus, the Rambam does not consider it a Mitzvah to take an oath in God’s name, no matter the motivation. Rather, it is an option permitted to one who feels compelled to swear. 

The simple reading of the verse (Devarim 10:20) and Gemara Temurah (3b) seems to be in accordance with the Rambam though it might be read as referring to an optional oath but not a commandment. (The machlokes Rambam and Ramban is also discussed in Chinuch 435.)

The pashut peshat in the verse points to the idea that when one is so dedicated to God, that intuitively he would swear and exclaim using his name:

⁦⁦You must revere the LORD your God: only Him shall you worship, to Him shall you hold fast, and by His name shall you swear.

Some commentaries walk the line between the Ramban and the Rambam. That is most of the time one should avoid making an oath in God’s name, even if true. However, on rare occasions, if a certain spirit overcomes the person, then it is with the correct intent and what the verse meant.

This would seem to be the approach of Rabbenu Bechaye (Devarim 10:20):

⁦ובשמו תשבע, “and in His name you are to swear.” Moses means that anyone who has first acquired the attributes listed is entitled to mention the Lord’s name when swearing an oath. Under certain circumstances it becomes a positive commandment to swear an oath, especially when it involves glorifying the name of the Lord. Examples of such circumstances are Elijah who decreed on oath that no rain would fall (Kings I 17,1) in accordance with G’d’s threat (in Deut. 11,17), and the sage Choni Hamagal who swore not to leave the spot he was standing on until G’d would provide rain (Ta’anis 19).

Chizkuni (Devarim 10:20) says something similar but expands it to Any moment of passion where an oath will inspire you:

⁦ובשמו תשבע, “and by His name you shall swear.” This refers to your mouth, as opposed to your heart. If you were to ask that the third commandment of the Ten Commandments had indicated that oaths should be used very sparingly, that is quite true; however, if the need to swear an oath arises you must make sure to swear only by His name. Such situations are primarily when you are tempted to commit a transgression, and you use your oath to stop yourself from giving in to that temptation, as where Boaz did so (Rus 3:13).

Making an oath is one of the few acts within Torah life that remain subjective judgement calls. The act can be a Mitzvah or an Aveirah depending on your motivation.

One final idea from Sefer Chasidim (420):

In our days we should refrain from even making oaths to galvanize Mitzvah and Torah observance. Because in the times of the Talmud, they would rather die than violate their oath, but in our times there isn’t that degree of steadfastness.